How to Extend Class in Scala

Suraj P Feb 02, 2024
  1. Extend Class in Scala
  2. Extend Case Classes in Scala
  3. Conclusion
How to Extend Class in Scala

In this article, we will learn about extending classes in Scala.

Extend Class in Scala

In Scala, we extend a class to achieve one of the most important parts of object-oriented programming (OOP): inheritance. To extend a class in Scala, we have to use an extend keyword.

Syntax:

derived_class_name extends base_class_name
{

}

Let’s see an example to understand it better.

Example code:

class Parent
{
    var child1: String = "Tony"
    var child2: String = "Nat"

}

class childOne extends Parent
{
    var age = 23
    var gender = "Male"

    def display()
    {
        println("Name : " +child1)
        println("Age : "+age)
        println("Gender : "+gender)
    }



}

class childTwo extends Parent
{
    var age = 18
    var gender = "Female"

    def display()
    {
        println("Name : " +child2)
        println("Age : "+age)
        println("Gender : "+gender)
    }



}


object myClass{

    def main(args: Array[String])
    {
     val obj1 = new childOne()
     val obj2 = new childTwo()

     obj1.display()
     obj2.display()


    }
}

Output:

Name : Tony
Age : 23
Gender : Male
Name : Nat
Age : 18
Gender : Female

In the above code, we can observe classes childOne, and childTwo inherited data from the base class parent.

Extend Case Classes in Scala

Scala’s case class is a special kind of class that is good at modeling immutable data. They are very useful in pattern matching.

We add a case keyword before the class to make it a case class.

Example code one:

case class student (name:String, rollNo:Int)
object myClass
{

    def main(args: Array[String])
    {
        var st = student("Tony", 33)
        println("Name of the student is: " + st.name);
        println("Roll number of the student is: " + st.rollNo);
    }
}

Output:

Name of the student is: Tony
Roll number of the student is: 33

Now let’s see how to extend or inherit these case classes. Unlike normal classes, we have some restrictions here.

Example code two:

case class records(schoolID: Int)
case class student (name:String, rollNo:Int) extends records
object myClass
{

    def main(args: Array[String])
    {
        var st = student("Tony", 33)

    }
}

Output:

error: case class student has case ancestor records, but case-to-case inheritance is prohibited

The above code gives an error because case-to-case class inheritance is prohibited in Scala. According to Scala, the case class can only extend a normal class, an abstract class or a trait.

Example code three:

Here we try to extend an abstract class.

abstract class records(schoolName: String)
{
    def name:Unit
}
case class student (schoolName:String) extends records(schoolName)
{
    override def name:Unit = println(schoolName)
}
object myClass
{

    def main(args: Array[String])
    {

        var st = student("delhi public school")
        st.name
    }
}

Output:

delhi public school

Example code four:

Here we try to extend a trait.

trait information
{
  def id: Int
  def name: String
}
case class student (id:Int,name:String) extends information
{
    println(id+" "+name)
}
object myClass
{

    def main(args: Array[String])
    {

       val temp: information = student(230,"Tony")
    }
}

Output:

230 Tony

Conclusion

This article taught us how to extend a class in Scala; we also saw how to extend case classes and observed its restrictions when inheriting another class.

Author: Suraj P
Suraj P avatar Suraj P avatar

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

LinkedIn GitHub