![]() |
VOOZH | about |
In any object-oriented programming language, Overriding is a feature that allows a subclass to provide a specific implementation of a method or field that is already provided by one of its super-classes. In Scala, Overriding is further explicitly stated in comparison to Overriding in Java, as here both methods as well as Fields can be overridden but it has a few restrictions which are obligatory to follow.
The rules for the field overriding are as follows:
Now, lets see some examples to illustrate these restrictions.
Example :
It is a circle. It is a square.
Now, we can see in above example that val is utilized in both super class and subclass so, overriding of the field was practicable otherwise it would have thrown an error. Here, the field in the super class is val which is overridden in the sub-classes using the override keyword.
Example :
It is the same example as above but here errors are found as here, var is used in the sub-classes to override the fields which is not possible as val cannot be overridden by the var as stated above.
Example :
Here, var is utilized for overriding fields which is not possible as stated in the above rules so, an error is found.