9

Introduction of Inheritance and its types in Scala - Knoldus Blogs

 3 years ago
source link: https://blog.knoldus.com/inheritanceand-its-types-in-scala/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Knoldus Blog Audio
Reading Time: 5 minutes

One of the important topics of Object-Oriented Programming is Inheritance. Inheritance allows us to define a class in terms of another class, which allows us in the reusability of the code. Here I’ll be giving you an introduction to inheritance in Scala.

What is Inheritance in Scala ?

It is the instrument in Scala by which one class is permitted to acquire the features(fields and methods) for another class.

The keyword used for inheritance is (extend).

SYNTAX

class parent_class_name extends child_class_name{

Important Terminology

Superclass: The class whose highlights are acquired is known as the superclass (or base class or parent class).

Subclass: The class that inherits another class is known as a subclass (or an inferred class, extended class, or youth class).

Reusable: in Reusability, once we wish to make a new class and there’s already a class that features a number of the code that we would like, we will derive our new class from the existing class we are reusing the fields and methods of the existing class.

Example


class Parent{
  var Name: String = "Rahul"
}
class Child extends Parent {
  var Age: Int = 5

  def details() {
    println("Parent Name: " + Name)
    println("Age: " + Age)
  }
}
object Main {
  def main(args: Array[String]) {
    var obj = new Child();
    obj.details();
  }}

Types of Inheritance

There are 5 types of inheritance in scala

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

Single Inheritance

single inheritance is the most effortless of the inheritance models. This is utilized when you have a class that has essential qualities and you need to take more classes that have every one of the essential properties and some particular characteristics. In single Inheritance, the determined class inherits the highlights of one base class.

 class Parent
{
  var ParentName: String = "Rahul"
}
class Child extends Parent
{
  var Age: Int = 24
  def details()
  {
    println("ParentName: " +ParentName)
    println("Age: " +Age);
  }
}

object Main
{
  def main(args: Array[String])
  {

    var obj = new Child()
    obj.details()
  }
}
OUTPUT

Multilevel Inheritance

In the Multilevel inheritance, a derived class will acquire a base class, and just as the derived class additionally go about as the base class to another class

In the Above Diagram A fills in as a base category for the derived class B, which so fills in as a base category for the derived category C.

class Parents
{
  var Name: String = "Aditya"
}

class ChildNo1 extends Parents
{
  var Age: Int = 24
}
class ChildNo2 extends ChildNo1
{
  def details(){
    println("Name: " +Name)
    println("Age: " +Age)
  }
}
object Main{
  def main(args: Array[String])
  {
    var obj= new ChildNo2()
    obj.details()
  }
}

OUTPUT

Hierarchical Inheritance

In Hierarchical Inheritance, one class fills in as a superclass (base class) for more than one subclass. When more than one classes inherit an equivalent class then this is called various Hierarchical Inheritance. For example classes B, C and D expand class A.

class Parent
{
  var Name1: String = "Rahul"
  var Name2: String = "Shreya"
}
class ChildNo1 extends Parent
{
  var Age: Int = 24
  def details1()
  {
    println(" Name: " +Name1)
    println(" Age: " +Age)
  }
}
class ChildNo2 extends Parent
{
  var Weight: Int = 70

  def details2()
  {
    println(" Name: " +Name2);
    println(" Weight: " +Weight);
  }
}

object main{

  def main(args: Array[String])
  {

    val object1 = new ChildNo1()
    val object2 = new ChildNo2()
    object1.details1()
    object2.details2()
  }}

OUTPUT

MULTIPLE INHERITANCE

Multiple Inheritance: In Multiple inheritances, one class can have more than one superclass and acquire features from all parent classes. Scala doesn’t uphold multiple inheritances with classes, however, it can be accomplished by traits.

EXAMPLE

trait Ram {
  
    def method1()
  }
trait Shyam{
  
    def method2()
  }

class Parents extends Ram with Shyam{
  
    def method1()
    {
      println("Ram")
    }

    def method2()
    {
      println("Shyam")
    }
  }
  object Main{
    def main(args: Array[String])
    {
      
      var obj = new Parents()
      obj.method1()
      obj.method2()
    }}

OUTPUT

HYBRID INHERITANCE

Hybrid Inheritance: It is a mix of at least two of the above kinds of inheritance. Since not possible with classes. In Scala, we can accomplish hybrid inheritance just through traits

EXAMPLE

class A {
  var numA: Int = 0;

  def setA(n: Int) {
    numA = n;
  }

  def printA() {
    printf("numA: %d\n", numA);
  }
}

class B extends A {
  var numB: Int = 0;

  def setB(n: Int) {
    numB = n;
  }

  def printB() {
    printf("numB: %d\n", numB);
  }
}

class C extends B {
  var numC: Int = 0;

  def setC(n: Int) {
    numC = n;
  }

  def printC() {
    printf("numC: %d\n", numC);
  }
}

class D extends A {
  var numD: Int = 0;

  def setD(n: Int) {
    numD = n;
  }

  def printD() {
    printf("numD: %d\n", numD);
  }
}

object Main {
  def main(args: Array[String]) {
    var obj1 = new C();
    var obj2 = new D();

    obj1.setA(10);
    obj1.setB(20);
    obj1.setC(30);

    obj1.printA();
    obj1.printB();
    obj1.printC();

    obj2.setA(40);
    obj2.setD(50);

    obj2.printA();
    obj2.printD();
  }
}

OUTPUT

This image has an empty alt attribute; its file name is screenshot-from-2021-07-16-12-50-24.jpg

References

https://docs.scala-lang.org/
https://www.tutorialspoint.com/scala/index.htm
https://www.geeksforgeeks.org/scala-programming-language/?ref=ghme/?ref=ghm

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK