- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Method Overloading and Method Overriding in Java
Method Overloading in Java
When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). 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 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); } }
Output
On execution, it will produce the following output −
60 150
Method Overriding in Java
In method overriding, 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 same.
Example of Method Overriding
Let's take an example to understand how method overriding works in Java −
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(); } }
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.
Output
On execution, it will produce the following output −
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 known as compile-time polymorphism. |
Method overriding is known as runtime polymorphism. |
For overloading to come into 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 different number of parameters. If both the methods have the same number of parameters, then their type must be different. |
Both the methods must have the same number of parameters with the same type. |
Conclusion
Overloading and Overriding are concepts in object-oriented programming that are used to improve the readability and reusability of the programs. Method overloading is a type of static polymorphism. In Method overloading, we can define multiple methods with the same name but with different parameters.
Method Overriding is a mechanism to achieve polymorphism where the super class and the sub-class have same methods, including the parameters and signature. The JVM calls the respective method based on the object used to call the method.
- Related Articles
- Difference Between Method Overloading and Method Overriding in Python
- What is the difference between method hiding and method overriding in Java?
- Difference between Method Overriding and Method Hiding in C#
- What is the difference between method overloading and method hiding in Java?
- Difference Between Function Overloading and Overriding in C++
- Method overriding in Java
- Method overloading in Java
- Using Method Overloading in Java
- method overloading and type promotion in Java
- Method Overloading and null error in Java
- What is the difference between function overriding and method hiding in C#?
- overriding method different package in java
- When Method Overriding occurs in Java?
- Rules for Java method overriding
- Method Overloading and Ambiguity in Varargs in Java
