Read Also : Difference between String, StringBuilder and StringBuffer
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).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.
Method Overriding and Method Overloading Example
Below code snippets will clear all the concepts of method overloading and method overridingpublic class Animal { public void eat() { System.out.println("Generic Animal Eating Generically"); } } public class Horse extends Animal { public void eat() { System.out.println("Horse eating hay "); } public void eat(String s) { System.out.println("Horse eating " + s); } }
1. If you try to execute below code
Animal a = new Animal(); a.eat();
Output : Generic Animal Eating Generically
2. If you try to execute below code
Horse h = new Horse(); h.eat();
Output : Horse eating hay
3. If you try to execute below code
Animal ah = new Horse(); ah.eat();
Output : Horse eating hay
Polymorphism works—the actual object type (Horse), not the reference type (Animal), is used to determine which eat() is called.
4. If you try to execute below code
Horse he = new Horse(); he.eat("Apples");
Output : Horse eating Apples
The overloaded eat(String s) method is invoked.
5. If you try to execute below code
Animal a2 = new Animal(); a2.eat("treats");
Output : Compiler Error
Compiler error! Compiler sees that Animal class doesn't have an eat() method that takes a String.
6. If you try to execute below code
Animal ah2 = new Horse(); ah2.eat("Carrots");
Output : Compiler Error
Compiler error! Compiler still looks only at the reference, and sees that Animal doesn’t have an eat() method that takes a String. Compiler doesn’t care that the actual object might be a Horse at runtime.
More Questions on Method Overriding and Method Overloading
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.
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.