Updated April 7, 2023
Introduction to Inheritance in Scala
In this article, we will discuss about the concept of Inheritance. Firstly, Let’s start with the definition of Inheritance. In plain English, Inheritance means “Heritage Property” which can be utilized by their descendant. Similarly, In Object-Oriented Programming, Inheritance is the mechanism of reusing an existing functionality. i.e., utilizing the methods from super class/trait to sub class.
Why do We Use Inheritance in Scala?
It is handy, when integrating a system with various sub-systems which in-turn saves time and avoids complexity.
Secondly, let’s understand the terminologies.
- Inheritance-Super Class: It is a Parent/Base Class with some functionality.
- Inheritance-Sub Class: It is a Child Class that can extend the Super Class and uses its functionality.
- Scala- “extends”: It is a Keyword to extend the existing class and use/override its methods.
- Scala- “with”: It is a Keyword to extend more than one class and use/override its methods.
Limitations within the Class in Scala:
- No Class can have an unimplemented method unless it is an abstract
- We can have unimplemented methods in trait
Examples to Implement of Inheritance in Scala
Below are the examples of Inheritance in Scala:
Example #1
Class with Unimplemented Method.
Code:
//scala 2.11.7
class motor {
def samplePrint
}
class motorChild extends motor { def samplePrint() {
}
def samplePrint1() {
println("This is an motorChild Class")
}
}
object InheritanceMain {
def main (args: Array [String]) { val m = new motorChild
m.samplePrint1()
}
}
Output:
Example #2
Trait with Unimplemented Methods.
Code:
//scala 2.11.7
trait motor {
def samplePrint
}
class motorChild extends motor { def samplePrint() {
}
def samplePrint1() {
println("This is an motor child class")
}
}
object InheritanceMain {
def main (args: Array [String]) { val m = new motorChild
m.samplePrint1()
}
}
Output:
Inheritance in Scala
By default, classes are extensible in Scala. Hence, it supports inheritance concept of OOP.
as how “we overcame it by using Interface in Java”.
Syntax:
trait motor1 { def car
def truck
}
trait motor2 { def bicycle
}
class manufacturing extends motor1 with motor2 { def car () {
}
def bicycle() {
}
def truck() {
}
}
We will discuss more about the types of Inheritance and how does it work in detail as follows:
Types of Inheritance
Below are the different types of Inheritance:
- Single Inheritance
- Multi-level Inheritance
- Multiple Inheritance
- Hybrid Inheritance
1. Single Inheritance
In Single Inheritance, only one sub class can access the features of one Super Class.
Flowchart:
Code:
//scala 2.11.7
class Motor { val car=100
}
class Cars extends Motor { def test {
println(s"value of car from super class is: $car")
}
}
object InheritanceMain {
def main (args: Array [String]) { val m = new Cars m.test
}
}
Output:
2. Multi-level Inheritance
In Multi-level Inheritance, there are various levels of child sub classes can access methods from their own parent super class till its actual Super Class by using “extends” keyword.
Flowchart:
Code:
//scala 2.11.7
class Motor { val car = 100
}
class Cars extends Motor { val carMotor = 200
def test () { println(s"value of car from Super Class: $car") }
}
class CarsChild extends Cars {
def test1 () { println(s"value of car from Super Class: $car & value of carMotor from Cars class:
$carMotor") }
}
object InheritanceMain {
def main (args: Array [String]) { val m = new CarsChild m.test m.test1
}
}
Output:
3. Multiple Inheritance
- In Multiple Inheritance, more than one sub class can access the methods of a Super
- This type of inheritance cannot be simply implemented using the normal classes, since Scala classes doesn’t allow us to declare the unimplemented methods in it.
- Hence, we can use the facts of “mixed in trait” to get our work.
Example:
If we try to use the classes to implement Multiple Inheritance in Scala, we will be encountering an exception as below:
Flowchart:
Example 1: Scala code to implement multiple inheritance using Only Classes.
Code:
//scala 2.11.7
class Motor {
def samplePrint() {}
}
class Cars {
def car () { println("this is an Cars class") }
}
class Bicycles {
def biCycle () { println("this is an Bicycle class") }
}
class Trucks extends Motor with Cars with Bicycles { def truck () { println("this is an Trucks class") }
}
object InheritanceMain {
def main (args: Array [String]) { val m = new Trucks m.truck m.biCycle m.car
}
}
Output:
Example 2: Scala code to implement multiple inheritance using class & traits.
Code:
//scala 2.11.7
class Motor {
def samplePrint() {}
}
trait Cars {
def car () { println("this is an Cars class") }
}
trait Bicycles {
def biCycle () { println("this is an Bicycle class") }
}
class Trucks extends Motor with Cars with Bicycles { def truck () { println("this is an Trucks class") }
}
object InheritanceMain {
def main (args: Array [String]) {
val m = new Trucks m.truck
m.biCycle m.car
}
}
Output:
Extend Class -> with trait1 with trait2..
It will throw us an exception, if we do not follow this order.
4. Hybrid Inheritance
- As the name suggests, this type of inheritance doesn’t have a style of its own but results when mixing any two types of inheritance together.
- For example, combining both Single & Multiple inheritance together or Single& Multi-level inheritance together or Multiple & Multi-level inheritance.
Overriding an existing Method present in the Parent Class:
- When we try to implement the method, which has been already implemented in the Super Class, we might end up with an
- In a situation like this, we should use “override” keyword, in order to avoid this
Example 1: without override keyword
Code:
//scala 2.11.7 class Motor {
def samplePrint() {
println("this is an Motor Class Method ")
}
}
class Trucks extends Motor { def samplePrint() {
println("this is an Trucks class Method")
}
}
object InheritanceMain {
def main (args: Array[String]) { val m = new Trucks m.samplePrint
}
}
Output:
Example 2: with override keyword
Code:
//scala 2.11.7
class Motor {
def samplePrint() {
println("this is an Motor Class Method ")
}
}
class Trucks extends Motor { override def samplePrint() {
println("this is an Trucks class Method")
}
}
object InheritanceMain {
def main (args: Array[String]) { val m = new Trucks m.samplePrint
}
}
Output:
Advantages
The main advantages of inheritance are,
- Code Reusability
- Code Readability
Summary
As inheritance plays an important role in Object Oriented Programming, by avoiding the need to write the duplication of code everywhere. Instead sub classes can use the functionality of super class(es), just by extending it, without giving the chance to rewrite the code again and again.
Also, Scala makes it even simpler to implement this important OOP concept using trait.
Hints:
- “extends” keyword is used to extend the existing class/trait.
- Methods inside trait can either be declared or All the unimplemented methods of Super Class should be implemented in the Sub.
- It is not necessary to override the implemented methods of Super But while overriding the implemented methods of Super Class, use “override” Keyword in the Sub Class.
- to use more than one trait use “with” keyword.
Recommended Articles
We hope that this EDUCBA information on “Inheritance in Scala” was beneficial to you. You can view EDUCBA’s recommended articles for more information.