Scala Inheritance – Syntax, Example & Types of Inheritance in Scala

Free Scala course with real-time projects Start Now!!

In our last Scala Tutorial, we discuss Scala Case Class. Now, we will discuss Scala inheritance with syntax and examples. Along with this, we will cover different types of inheritance in Scala Programming. At last, we will see examples of multilevel inheritance and Hierarchical Inheritance in Scala.

Scala Inheritance – Syntax, Example & Types of Inheritance in Scala

Scala Inheritance

When a class inherits from another, it means it extends another. We use the ‘extends’ keyword for this. This lets a class inherit members from the one it extends and lets us reuse code.
Let’s Learn Scala Map with Examples Quickly & Effectively
The class that extends is the subclass, the child class, or the derived class. The other class is the superclass, the parent class, or the base class.

Scala Inheritance

Scala Inheritance

It is an IS-A relationship. You can also call it a generalization. A Student is a Person.

A syntax of Scala Inheritance

To carry out Scala inheritance, we use the keyword ‘extends’:

class Student extends Person(){
/*your code
*goes here
*/

Let’s Discuss Scala String Interpolation – s String, f String and raw string Interpolator

Scala Inheritance Example

Let’s take an example of Inheritance.

scala> class Person{
| var SSN:String="999-32-7869"
| }
defined class Person
scala> class Student extends Person{
| var enrolment_no:String="0812CS141028"
| println("SSN: "+SSN)
| println("Enrolment Number: "+enrolment_no)
| }
defined class Student
scala> new Student()
SSN: 999-32-7869
Enrolment Number: 0812CS141028
res0: Student = Student@42cfd794

In this example, we have two classes- Person and Student. We make Student extend Person. This means a Student is a Person.
Since Student extends Person, it inherits the attribute holding the social security number. In class Student, we print SSN and enrolment_no. Finally, we create an object of class Student.

Types of Inheritance in Scala

Scala supports five kinds of inheritance:

Scala Inheritance

Types of Inheritance in Scala

a. Single-level Inheritance in Scala

Scala Single-level inheritance is when one class inherits from a single other class.

Scala Inheritance

Single-level Inheritance

Read about Scala Access Modifiers: Public, Private and Protected Members

b. Multilevel Inheritance in Scala

When one class extends another, which in turn extends another, it is an instance of multilevel inheritance.

Scala Inheritance

Multilevel Inheritance in Scala

Let’s Scala if-else Statements with examples

c. Multiple Inheritance in Scala

When one class inherits from multiple base classes, it is a case of multiple inheritances.

Scala Inheritance

Multiple Inheritance in Scala

d. Hierarchical Inheritance in Scala

When more than one class inherits from one base class, it is said to be hierarchical inheritance.

Scala Inheritance

Multiple Inheritance in Scala

e. Hybrid Inheritance in Scala

Scala Hybrid inheritance is a combination of at least two kinds of inheritance.

Scala Inheritance

Hybrid Inheritance in Scala

Let’s explore Scala Operator in detail

Multilevel Inheritance Example – 1

scala> class A{
| println("A")
| }
defined class A
scala> class B extends A{
| println("B")
| }
defined class B
scala> class C extends B{
| println("C")
| }
defined class C
scala> new C()
A
B
C
res1: C = C@347f8029

In this example, we observe that class C extends class B and class B extends A.

Example – 2

Take a look at this code:

object Multiple{
        def main(args: Array[String]):Unit={
           trait A{
                   var length:Int= _
                   def action={
                     length=length+5
                  }
             }
           trait B{
                   var height:Int = _
                   def action={
                     height = height + 1
                  }
             }
           class C extends A with B{
                   length=3;
                   height+=6;
                   override def action={
                            super[A].action
                            super[B].action
                   }
              }
                var c=new C
                c.action
                println(c.height)
                println(c.length)
             }
}

Here, we make use of traits to let class C inherit from traits A and B.

Example- Hierarchical Inheritance

scala> class A{
| println("A")
| }
defined class A
scala> class B extends A{
| println("B")
| }
defined class B
scala> class C extends A{
| println("C")
| }
defined class C
scala> new B()
A
B
res2: B = B@64c1a76e
scala> new C()
A
C
res3: C = C@193f5509

In this example, classes B and C inherit from class A.
Do you know Scala Job Opportunities: Profile, Salary & Top Organizations

Conclusion

Hence, we studied what is Inheritance in Scala with example and syntax. In addition, we saw different types of inheritance. If you have a doubt, feel free to ask in the comment section.
Related Topic- Scala Closures with Examples 
For reference

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

4 Responses

  1. Beginner says:

    The tutorial on inheritance is really nice.
    If you could add an example on Multiple inheritance inline with the explanation would make it more worthy.
    Thanks 🙂

    • DataFlair Team says:

      Thanks for commenting on Scala inheritance. We have added an example of Scala Multiple Inheritance in the above article, we can check it.
      Regards,
      DataFlair

  2. vishal shrivastava says:

    Multiple Inheritance in Scala: with due respect Sir. I have one query
    do Scala follow Multiple Inheritance ??

Leave a Reply

Your email address will not be published. Required fields are marked *