Method Overloading
In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different.
When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java supports polymorphism.
When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java supports polymorphism.
Method Overriding
In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.
When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden.
When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden.
Difference between Method Overloading and Method Overriding are shown below:-
Sr No. | Method Overloading | Method Overriding |
1 | It is Compile Time Polymorphism. | It is Run Time Polymorphism. |
2 | Method Overloading is Performed within class. | Method Overriding occurs in two classes that has inheritance relationship. |
3 | Here, Parameters must be different. | Here, Parameters must be same. |
4 | In Method Overloading, return type may or may not be same. | Whereas in Overriding, Return type must be same. |
5 | Same name and Different Signature. | Same name and same Signature. |
0 Comments