Method Overriding in Scala
Last Updated :
25 Apr, 2019
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 :
Scala
// Scala program of method overriding
// Creating a super class
class School
{
// Method defined
def NumberOfStudents()=
{
0 // Utilized for returning an Integer
<