Inheritance in Scala - How to extend a class in Scala?

Here, we will learn about Inheritance in Scala. Inheritance is a way to extend classes. We will learn about inheritance its syntax, examples, and types.
Submitted by Shivang Yadav, on December 12, 2020

Inheritance in Scala is the concept of inheriting values and properties by one object from another object.

Scala programming language is a programming language that integrates features of both object-oriented programming and function programming supports inheritance too, as it is a really important feature of OOP's concept.

Before going ahead, let's see some key terms related to inheritance in Scala,

  • Super Class is also known as base class or parent class. It is the class whose features are inherited by other classes.
  • Sub Class is also known as child class, derived class, or extended class. It is the class that inherits features from other classes.

To implement the concepts of inheritance we need to understand the working of some keywords in Scala:

  • extends: The extends keyword in Scala is used to inherit features of one class by another class.
    baseClass extends parentClass
    This extends keyword is used to inherit class while creating a new one.
  • With: the with keyword in Scala is used when we need to inherit more than one class by another class.

Types of Inheritances in Scala

While inheriting classes in Scala, there can be multiple ways to inherit classes.

Here are types of inheritance in Scala:

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

1. Single Inheritance

Single inheritance is a type of inheritance in which one class is inherited by another class.

The concept is depicted in the following diagram,

Single Inheritance

Here, in single inheritance, one class is simply inherited by another class in Scala.

Program to illustrate the working of inheritance in Scala

// Program to illustrate the working of single inheritance in Scala...

class class1{
var value1 = 935
}

class class2 extends class1{
    val value2 = 54
    def printValues(){
        println("Value 1: " + value1 )
        println("Value 2: " + value2 )
        println("Sum: " + (value1 + value2))
    }
}

object MyClass {
    def main(args: Array[String]) {
        val object1 = new class2(); 
        object1.printValues()        
    }
}

Output:

Value 1: 935
Value 2: 54
Sum: 989

2. Multiple Inheritance

Multiple inheritance is a type of inheritance in which multiple classes are inherited by one class at a time.

The concept is depicted in the following diagram,

Multiple Inheritance

Here, one class Derived Class derives two different classes Base class 1 and Base Class 2.

Program to illustrate the working of Multiple inheritance in Scala

// Program to illustrate the working of 
// multiple inheritance in Scala...

trait baseClass1{
    var value1 = 935
}

trait baseClass2{
    var value2 = 43
}

class derivedClass extends baseClass1 with baseClass2{
    def printValues(){
        println("Derived Class")
        println("Value 1 : " + value1 )
        println("Value 2 : " + value2 )
        println("Concatination : " + (value1 + value2) )
    }
}

object MyClass {
    def main(args: Array[String]) {
        val object1 = new derivedClass(); 
        object1.printValues()       
    }
}

Output:

Derived Class
Value 1 : 935
Value 2 : 43
Concatination : 978

3. Multilevel Inheritance

Multi-Level inheritance is a type of inheritance in which one class is inherited by another class which is in turn inherited by the third class.

The concept is depicted in the following diagram,

Multilevel Inheritance

Here, is a diagram that depicts a multilevel inheritance in which two single inheritances are joined together.

Program to illustrate the working of our multi-level inheritance in Scala

// Program to illustrate the working of multilevel inheritance in Scala...
class class1{
    var value1 = 935
}

class class2 extends class1{
    val value2 = 54
}

class class3 extends class2{
    def printValues(){
        println("Value 1: " + value1 )
        println("Value 2: " + value2 )
        println("Sum: " + (value1 + value2))
    }
}

object MyClass {
    def main(args: Array[String]) {
        val object1 = new class3(); 
        object1.printValues()
    }
}

Output:

Value 1: 935
Value 2: 54
Sum: 989

4. Hierarchical Inheritance

Hierarchical inheritance is a type of inheritance in which one class is inherited by more than one class.

The concept is depicted in the following diagram,

Hierarchical Inheritance

Here, two derived classes Derived Class 1 and Derived Class 2 are inherited from Base Class

Program to illustrate the working of Hierarchical inheritance in Scala

// Program to illustrate the working of 
// Hierarchical inheritance in Scala...

class baseClass{
    var value1 = 935
}

class derivedClass1 extends baseClass{
    def printValues(){
        println("\nDerived Class 1")
        val value2 = 54
        println("Value 1: " + value1)
        println("Value 2: " + value2)
        println("Sum: " + (value1 + value2))
    }
}

class derivedClass2 extends baseClass{
    def printValues(){
        val value2 = 341
        println("\nDerived Class 2")
        println("Value 1 : " + value1 )
        println("Value 2 : " + value2 )
        println("Concatination : " + value1 + value2 )
    }
}

object MyClass {
    def main(args: Array[String]) {
        val object1 = new derivedClass1(); 
        object1.printValues()        
        val object2 = new derivedClass2(); 
        object2.printValues()        
    }
}

Output:

Derived Class 1
Value 1: 935
Value 2: 54
Sum: 989

Derived Class 2
Value 1 : 935
Value 2 : 341
Concatination : 935341

5. Hybrid Inheritance

Hybrid Inheritance is a special type of inheritance which is a combination of hierarchical and multiple inheritance in Scala.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.