What is method overriding in Java?
Modifying a superclass method in the subclass is called method overriding. Using method overriding, we can change the superclass method according to the requirements of the subclass.
There are 5 main rules you should keep in mind while overriding a method. They are
i) Name of the method must be the same as that of a super class method.
ii) The return type of overridden method must be compatible with the method being overridden. That is if a method has primitive type as its return type then it must be overridden with primitive type only and if a method has derived type as its return type then it must be overridden with the same type or it’s subclass types.
iii) You must not reduce the visibility of a method while overriding.
iv) You must not change the parameter list of a method while overriding.
v) You cannot increase the scope of exceptions while overriding a method with throws clause.
No comments:
Post a Comment