
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Method Overloading vs Method Overriding in Java
Java supports overloading, i.e., in a Java class, we can have two different methods with the same name and different parameter lists (different number of parameters with same type or, same number of parameters with different types).
Whereas, method overriding is a concept where the super class and the sub-class have the same methods, including the parameters and signature. The JVM calls the respective method based on the object used to call the method.
Method Overloading in Java
When a class has two or more methods with the same name but different parameters, the respective method is called based on the arguments passed. The respective method body is dynamically bound to the calling line. This mechanism is known as method overloading.
Example of Method Overloading
If you observe the following example, here we have created a class called Sample, and this class has two methods with the same name (add) and return type. The only difference is the parameters they accept (one method accepts two integer variables, and the other accepts three integer variables).
When you invoke the add() method based on the parameters you pass respective method body gets executed.
public class Sample { public static void add(int a, int b) { System.out.println(a+b); } public static void add(int a, int b, int c) { System.out.println(a+b+c); } public static void main(String args[]) { Sample obj = new Sample(); obj.add(20, 40); obj.add(40, 50, 60); } }
On execution, it will display the following result:
60 150
Method Overriding in Java
In method overriding, the super class and subclass have methods with the same name, including parameters. JVM calls the respective method based on the object used to call the method. In overriding, the return types should also be the same.
Example of Method Overriding
Let's take an example to understand how method overriding works in Java. Here, we have a SuperClass and a SubClass. Both the classes have a method called Sample() with the same signature. In the main class, we created obj1 for SuperClass and obj2 for SubClass. The JVM calls the respective method based on the object used to call the method -
class SuperClass { public static void sample() { System.out.println("Method of the super class"); } } public class SubClass extends SuperClass { public static void sample() { System.out.println("Method of the sub class"); } public static void main(String args[]) { SuperClass obj1 = new SubClass(); SubClass obj2 = new SubClass(); obj1.sample(); obj2.sample(); } }
Following is the output of the above class -
Method of the super class Method of the sub class
Difference between Overloading and Overriding
The following table highlights the major differences between Method Overloading and Method Overriding -
Method Overloading |
Method Overriding |
---|---|
Method overloading is where we can have multiple methods with the same name and different parameter lists within a single class. |
Method overriding is where we have multiple methods with the same name and parameter lists, but these exist in the super class and subclasses, respectively. |
For overloading to come into the picture, there must be at least two methods of the same name. |
For overriding to work, we need to have at least one method with the same name in both the parent class as well as the child class. |
The methods must have a different number of parameters. If both methods have the same number of parameters, then their type must be different. |
Both methods must have the same number of parameters of the same type. |
Method overloading is possible with static methods. | Overriding with static methods is called method hiding. The subclass (static) method hides the method in the super class. If both have the same signature and both are declared static. |
Here, the method to be called is determined at compile time. | Here, the method to be called is determined at run time. |