Method Overriding in Scala
Method Overriding in Scala is identical to the method overriding in Java but in Scala, the overriding features are further elaborated as here, both methods as well as var or val can be overridden. If a subclass has the method name identical to the method name defined in the parent class then it is known to be Method Overriding i.e, the sub-classes which are inherited by the declared super class, overrides the method defined in the super class utilizing the override keyword.
Flow chart of method overriding
Lets, see the flow chart of the method overriding in order to visualize it explicitly.
Here, in the diagram stated above School is the super-class which has a method defined in it which is named as NumberOfStudents() and this method is overridden by the sub-classes i.e, class 1, class 2, class 3. so, all the sub-classes has the same named method as defined in the super-class.
When to apply Method Overriding ?
when a subclass wishes to impart a particular implementation for the method defined in the parent class then that subclass overrides the defined method from the parent class. When we wish to reconstruct the method defined in the super class then we can apply method overriding.
Let’s see an example which is related to the diagram mentioned above of the method overriding.
Example :
class School
{
def NumberOfStudents() =
{
0
}
}
class class _ 1 extends School
{
override def NumberOfStudents() =
{
30
}
}
class class _ 2 extends School
{
override def NumberOfStudents() =
{
32
}
}
class class _ 3 extends School
{
override def NumberOfStudents() =
{
29
}
}
object GfG
{
def main(args : Array[String])
{
var x = new class _ 1 ()
var y = new class _ 2 ()
var z = new class _ 3 ()
println( "Number of students in class 1 : " +
x.NumberOfStudents())
println( "Number of students in class 2 : " +
y.NumberOfStudents())
println( "Number of students in class 3 : " +
z.NumberOfStudents())
}
}
|
Output:
Number of students in class 1 : 30
Number of students in class 2 : 32
Number of students in class 3 : 29
In the above example, we have a class named School which defines a method NumberOfStudents() and we have three classes i.e, class_1, class_2 and class_3 which inherit from the super-class School and these sub-classes overrides the method defined in the super-class.
Example :
class Shapes
{
def Area(l : Double, b : Double, r : Double) =
{
0.0
}
}
class Rectangle extends Shapes
{
override def Area(l : Double, b : Double, r : Double) =
{
(l * b)
}
}
class Circle extends Shapes
{
override def Area(l : Double, b : Double, r : Double) =
{
(( 3.14 )* r * r)
}
}
object GfG
{
def main(args : Array[String])
{
var rectangle = new Rectangle()
var circle = new Circle()
println(rectangle.Area( 3 , 11 , 4 ))
println(circle.Area( 1 , 7 , 10 ))
}
}
|
In the above example, Area is a method of the super-class which is to be overridden by the methods defined in the sub-classes. Here, we are finding the area but for different shapes using the same method name i.e, Area thus, we can say that this method overriding can be applied for same kind of operations but for different categories and it is worth noting that the methods must have same data types and same number of parameters as defined in the super class otherwise the compiler will throw an error. Though the order of the parameters in the method defined can be altered in the sub-classes when the method is overridden.
Rules for Method Overriding
There are a few restrictions that we need to follow for method overriding, these are as follows:
- For method overriding, one of the crucial rule is that the class which is overriding needs to utilize the modifier override or override annotation.
- Auxiliary constructors are not able to call the super-class constructors immediately. They can hardly call the primary constructors which in reversal will call the super-class constructor.
- In Method Overriding, we won’t be able to override a var with a def or val, otherwise it throws an error.
- Here, we won’t be able to override a val in the super-class by a var or def in the subclass, and if the var in the super-class is abstract then we can override it in the subclass.
- If a field is stated var then it can override the def which is defined in the super-class. The var can only override a getter or setter combination in the super-class.
Note:
- Auxiliary Constructors are defined like methods utilizing def and this keywords, where this is the name of the constructor.
- Primary Constructors begins from the beginning of the class definition and stretches the whole body of the class.
Lets see some examples now.
Example :
class Animal
{
def number()
{
println( "We have two animals" )
}
}
class Dog extends Animal
{
override def number()
{
println( "We have two dogs" )
}
}
object GfG
{
def main(args : Array[String])
{
var x = new Dog()
x.number()
}
}
|
Here, we have overridden the method utilizing the keyword override. In the above example, the super class Animal has a method named number which is overridden in the subclass Dog. So, the overridden method can be called by creating the object of the subclass.
Example :
class Students( var rank : Int, var name : String)
{
override def toString() : String =
{
" The rank of " +name+ " is : " +rank
}
}
class newStudents(rank : Int, name : String)
extends Students(rank, name){
}
object GfG extends App
{
val students = new Students( 1 , "Geeta Sharma" )
println(students)
val newstudents = new newStudents( 3 , "Priti Singh" )
println(newstudents)
}
|
Output:
The rank of Geeta Sharma is : 1
The rank of Priti Singh is : 3
Here, the constructor of the super-class i.e, Students is called from the primary constructor of the subclass i.e, newStudents so, the super-class constructor is called utilizing the keyword extends.
Note: The AnyRef class has a toString() method defined in it and as we know that every class is subclass of AnyRef class so, the method toString() is overridden using the keyword override.
Overriding vs Overloading
- In Scala, method overloading supplies us with a property which permits us to define methods of identical name but they have different parameters or data types whereas, method overriding permits us to redefine method body of the super class in the subclass of same name and same parameters or data types in order to alter the performance of the method.
- In Scala, method overriding uses override modifier in order to override a method defined in the super class whereas, method overloading does not requires any keyword or modifier, we just need to change, the order of the parameters used or the number of the parameters of the method or the data types of the parameters for method overloading.
Why do we need Method Overriding ?
In order to redefine a single method in different ways, we can do method overriding which helps us to do different operations with same method name. Like, in the diagram shown above has a super-class School which has a method named NumberOfStudents() which is overridden by the sub-classes to perform different actions.
Last Updated :
25 Apr, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...