The following table shows the main differences between 'Method Overloading' and 'Method Overriding' in Java Technology.
Method Overloading | Method Overriding | |
1 | Definition: Method Overloading deals with multiple methods in the same class with the same name but different method signature. | Definition: Method Overriding deals with two methods, one in the parent class and the other in the child class and has the same name and signatures. |
2 | Syntax: class sampleClass { public void DisplayMesg(String name) { ………. } public void DisplayMesg(String name, int roll) { ………. } } | Syntax: class parentClass { public void DisplayMesg(String name) { ………. } } Class childClass extends parentClass { public void DisplayMesg(String name) { ……….. } } |
3 | Consider both the above methods have the same method names but different method signatures, which means the methods are overloaded | Consider both the above methods have the same method names and the signature but the method in the subclass childClass overrides the method in the superclass parentClass |
4 | Overloading lets you define the same operation in different ways for different data. | Overriding lets you define the same operation in different ways for different object types |
5 | Example: class A { public void print(String name) { System.out.println(“Name=”+name); } public void print(String name, int roll) { System.out.println(“Name=”+name + “ ”+”Roll=”+roll); } } public class sampleTest { public static void main(String args[]) { A a1 = new A(); a1.print(“Java”); a1.print(“Java”,12); } } C:\> java sampleTest Name=java Name=java Roll=12 | Exmaple: class father { public void print(String msg) { System.out.println(msg); } } class son extends father { public void print(String msg) { System.out.println(msg); } public class sampleTest { public static void main(String args[]) { father a1 = new father(); a1.print(“I am father”); son b1 = new son(); b1.print(“I am son”); } } C:\> java sampleTest I am father I am son |
Related Articles:
Difference between static class loading and dynamic class loading in java
Difference between Array and ArrayList in Java
Difference between Abstract Class and Interface in Java
Difference between String and StringBuffer/StringBuilder in Java
Difference between HashMap and HashTable in Java
Difference between Csharp and Java
Difference between ArrayList and Vector in Java
Difference between ‘is-a’ relationship and ‘has-a’ relationship in Java
Difference between C++ and Java
Difference between static class loading and dynamic class loading in java
Difference between Array and ArrayList in Java
Difference between Abstract Class and Interface in Java
Difference between String and StringBuffer/StringBuilder in Java
Difference between HashMap and HashTable in Java
Difference between Csharp and Java
Difference between ArrayList and Vector in Java
Difference between ‘is-a’ relationship and ‘has-a’ relationship in Java
Difference between C++ and Java