Overloading Vs Overriding
Overloading vs Overriding
A Way of Implementing Polymorphism
Overloading
- Overloading refers to having more than one method with the same name in a class whereas overriding refers to redefining methods of the superclass in the subclass.
- The signature of the method in the subclass and superclass should be the same.
- And helps to implement polymorphism in OOPS world.
Overriding Methods
- Refers to redefining methods which have the same signature as the superclass in the subclass
- Are used to extend the functionality of the superclass methods
- Rules for overriding methods are:
- the order of arguments of the overriding method should be identical to that of the superclass method
- the return type of both the overridden and the overriding methods must be the same or co-variant
- overriding method cannot be less accessible or raise more exceptions than the method it overrides
Recent Comments