Difference between Overloading and Overriding is the most common interview question asked to the java developer. Before answering the question, first we will understand what is method overloading and what is method overriding with simple examples.
Read Also : Difference between String, StringBuilder and StringBuffer
Related Articles
What is Method Overloading with Simple Example
Method Overloading must have the same method name and different parameters (method signature) i.e either you need to change number of parameters or type of parameters or order of parameters.
Note: Return type is not part of the method signature. Hence, changing return type with two methods having same method name and parameters will result in compile time error.
What is Method Overriding with Simple Example
Method Overriding must have the same method name and same parameters (method signature).Interviewer: Is it possible that Method overriding can have different method signature.
For one scenario only, the answer to the above question is yes. It is possible if the return type is covariant.
Interviewer: Can we override static methods in java
If a subclass defines a static method with the same signature as a static method in the superclass, then the method of the subclass hides the one in the superclass.
6 Difference between Method Overloading and Method Overriding in java
1. Purpose : To increase the readability of the program, Method Overloading is used.
Method overriding is used to provide the specific implementation of the method. Child class overrides the super class method.
2. Number of Classes Involved : Method overloading is performed with in class.So only one class is involved.
Method overriding involves two classes that have IS-A relationship (inheritance).In other words, parent class with child class is required for method overriding.
3. Parameters : In method overloading , parameters must be different.
In method overriding , parameters must be same( i.e method signatures).(although parameters can be different for covariant return type).
4. Polymorphism type : Method overloading is an example of compile-type polymorphism. Method overriding is an example of run-type polymorphism.
5.Return type : Method overloading does not depend upon the return type of the method. Return type can be same or different in method overloading. Parameter must be different.
Method overriding must have same return type or covariant return type.
6. Private, static or final method : You can not override private, static or final method in java.
But you can overload private, static or final methods in java.
Recap : Difference between Method Overloading and Method Overriding
Method Overloading | Method Overriding | |
---|---|---|
Purpose | Increases Readability of the program | Provide specific implementation of the method |
Number of Classes Involved | 1 | 2 (Parent and child class) |
Parameters | Must be different | Must be same |
Polymorphism type | Compile time | Run time |
Return type | Does not depend on the return type | Must be same or have covariant return type |
Private, static or final method | Yes method overloading is possible | No, Method overriding is not possible |
That's it for today. Please let me know in comments if you have any questions regarding difference between method overloading and method overriding.