Updated March 16, 2023
Difference Between Overloading and Overriding
Before diving deep into the differences between Overloading vs Overriding, we need to understand what they actually are and the scenarios in which they are particularly used? Newbies in Java often get confused between the two, but they are totally different from each other and used in their specific scenarios. Method overloading is when we have multiple methods with the same name but different signatures. Signatures include the number of method parameters, the data type of parameters. The return type of method is not included in the signature. This is done to provide the functionality of reusing the same method name and increasing the programs’ readability. Method Overriding happens in the case of Inheritance, in which the derived class inherits the properties from the base class. It involves defining the same base class method in a derived class with the same parameter and return type to define any specific functionality/ implementation of that method in the derived class.
Consider a scenario in which a programmer needs to find the area of a geometric figure. The area of each figure varies from the other. For example, for finding the area of the square, only a single parameter ‘side’ is required; for a rectangle, two parameters, ‘length and breadth’ is required, whereas for circle ‘radius is required, which can be decimal. To solve this purpose, method overloading is done in which the method area has different parameters and datatype for an area of different figures. Think of the situation of the Banking system, though multiple methods and procedures for all the employees are the same except for some like interest rates for normal and senior citizens is different. In this case, Method Overriding is used having different implementations of method Interest Rates in both the Normal and Senior citizen class inheriting the base class ‘Banking Rates’.
Head to Head Comparison between Overloading and Overriding (Infographics)
Below is the Top 7 Comparison between Overloading vs Overriding:
Key Differences between Overloading and Overriding
Let us discuss some of the major key differences between Overloading vs Overriding:
- In method overloading, methods can have the same or different access specifiers/ modifiers in the method name, whereas in the Method Overriding method of base case (overridden method) must have a restricted access specifier than the method of a parent class. For example, if a base class’s method is protected, then the child method private is not allowed.
- When compared in terms of performance, overloading has better performance than overriding because method overloading is done at compile time.
- The return type of a method is never a part of method Overloading; hence, it does not matter if different overloaded methods have the same or different return type, whereas, in Method Overriding return type of both parent and base methods, a class needs to be exactly the same.
- When talking about binding, Overloading has static binding, whereas Overriding has dynamic binding.
- Exception thrown by method does not matter in the overloaded method if one method is throwing exception other overloaded methods can/ cannot throw the same or different exception, but in case of Overriding, Overriding method (method in derived class) cannot throw an exception of higher hierarchy than the overridden method (method in base class).
- All the specifiers like private, final and static cannot be used in Method Overriding, whereas all the access specifiers are allowed in method overloading.
Comparison Table of Overloading vs Overriding
The table below summarizes the comparisons between Overloading vs Overriding:
S.No. | Overloading | Overriding |
1 | Method overloading is done to have an enhanced definition of methods according to various situations. | Method Overriding is done in order to provide a specific implementation of methods defined in the parent class. |
2 | Method Overloading is done in a single class in which one class having different definitions of a method. | Method Overriding is done between two classes having an IS-A (Inheritance) relationship between them. |
3 | Method Overloading is done at compile-time, and hence it is known as Compile time Polymorphism. | Method Overriding is done at runtime, and hence it is known as runtime Polymorphism. |
4 | Parameter ordering, data type, and parameter count need to be different for Method Overloading. | Parameter ordering, data type, and count need to be the same for Method Overriding. |
5 | The return type of a method can be the same or different in the case of Method Overloading. It does not matter at all. | The return type of a method needs to be the same in both parent and child class in the case of Method Overriding. |
6 | Static methods can be overloaded, i.e. we can have different static methods overloaded in the same class. | Static methods can never be overridden, i.e., a static method in a parent class and base class has no relation in between them. |
7 | Private and final methods can be overloaded in a class, i.e. a class can have overloaded more than 1 private and final methods. | Private and final methods can never be overridden in a child class. |
Example of Method Overloading
Following is an example of method overloading:
Code:
public class Figures{
public int area(int side)
{
return side*side;
}
public int area(int length, int breadth) //method overloading
{
return length*breadth;
}
public static void main(String[] args)
{
Figures f = new Figures ();
System.out.println("Area of Square "+ f.area(10));
System.out.println("Area of Rectangle "+ f.area(12,10));
}
}
Output:
In the above example, the method ‘area()’ is overloaded and has different parameters in both the overloaded methods as the area needs to find out for both the square and rectangle but with different parameters.
Example of Method Overriding
Following is an example of method overriding:
Code:
public class BankRates{ // parent(base) class
void rates()
{
System.out.println(“Rates for normal citizen is 3.5%”);
}
}
class SeniorCitizen extends BankRates{ //child class inheriting parent class
void rates() // method overriding
{
System.out.println(“Rates for senior citizens is 4.5%”);
}
}
class Bank{
public static void main(String[] args)
{
SeniorCitizen sc = new BankRates();
sc.rates();
}
}
Output:
In the above example, method ‘rates()’ is overridden in the derived class ‘SeniorCitizen’ because we want the method rates in the class SeniorCitizen too but with a different implementation.
Conclusion
The above explanation clearly shows the difference between Overloading vs Overriding and the specific scenarios where these two are used. Before programming, one needs to understand these core concepts of Java as they form the basis of many things and understand more advanced concepts.
Recommended Articles
This has been a guide to the top difference between Overloading vs Overriding. Here we also discuss the Overloading vs Overriding key differences with infographics and comparison table. You may also have a look at the following articles to learn more –