Difference Between Method Overloading and Method Overriding
The object-Oriented Programming approach, popularly abbreviated as OOPS, is the basis of all modern programming languages. These languages adhere to and implement the concepts of object-oriented programming with very few exclusions. Two of those concepts are very crucial – method overloading and method overriding.
What is Method Overloading?
Method Overloading is the concept of defining two or more methods with the same name but different signatures. Either of these methods is called based on the number of parameters passed.
What is Method Overriding?
Method Overriding is the concept of defining two or more identical methods, having the same name and signatures. Either of these methods is called based on the calling object.
Head to Head Comparison Between Method Overloading and Method Overriding (Infographics)
Below are the top 10 differences between Method Overloading and Method Overriding:
Key Differences Between Method Overloading and Method Overriding
Following are the key differences between Method Overloading and Method Overriding.
- The most basic difference here is that overloading is achieved in the same class, whereas overriding requires a parent and a child class at the minimum.
- Another key concept to keep in mind is that overloaded methods are bound at compile time to the method calls. This means that the compiler knows which method to invoke before the code is run.
Overridden methods are bound at the run time. The calling object determines whether the parent class method must be invoked or the child class method. This may lead to more run-time exceptions if not handled properly. - Static methods can be overloaded but not overridden. This is because objects of the class do not call static methods. Method overriding is determined by the object invoking the method. Thus, static methods cannot be overridden.
Method Overloading and Method Overriding Comparison Table
Here is a comparison table showing similarities between Method Overloading and Method Overriding:
Basis of Comparison between Method Overloading and Method Overriding | Method Overloading | Method Overriding |
Definition | Method Overloading is the concept of defining two or more methods with the same name but different signatures. | Method Overriding is the concept of defining two or more identical methods, having the same name and signatures. |
Method Binding | Compile Time: The binding of overloaded method definitions with the respective method calls is done at compile time. | Run Time: The binding of overridden method definitions with their method calls is done at run time. |
Type of Method Binding | Static Binding | Dynamic Binding |
Class Restriction | Method overloading can be achieved in the same class or in different classes. No restrictions. | Method Overriding is achieved in different classes. These classes have a parent-child relationship. |
Signature Restriction | Overloaded methods must differ in their signatures. The number of parameters or the type of parameters or the order of parameters must be different. | Overridden methods can have the exact same signatures, no restrictions. |
Static Method Restriction | Static Methods can be overloaded. This allows a class to have more than one static method with the same name but different signatures. | Static Methods cannot be overridden. If a child class has a static method with the same name and signature as the parent class, it is considered as a separate method and does not override the parent class method. |
Method Return Type | The return type of overloaded methods does not matter. Overloaded methods may or may not have the same return type. However, methods with the same name and signatures but differing only in return types cannot be overloaded. | Overridden methods may have more specific return types. A parent class method may return a parent class object, and a child class method, overriding the parent class method, may return a child class object. |
Usage | Overloading is done to implement different method logics while keeping the method name the same. | It is mostly done to write a specific implementation of a method inherited from the parent class. |
Benefits | Increases program readability. Increases Code reusability. |
Helps write code logic to handle specific scenarios by passing the usual code. |
Related OOPS Concept | Closely knit with polymorphism: The same method can perform different actions based on the difference in parameters. | Closely knit with an inheritance: A child class may deviate from parent logic to handle one specific scenario while still inheriting other scenarios. |
Examples of Method Overloading and Method Overriding in Java
Following are the examples of Method Overloading and Method Overriding in Java.
Example #1
Code:
class Hello
{
public void SayHello()
{
System.out.println("Hello World!");
}
//overloading method
public void SayHello(String name)
{
System.out.println("Hello "+name+"!");
}
}
class Main
{
public static void main(String args[])
{
Hello obj = new Hello();
obj.SayHello();
obj.SayHello("Charlie");
}
}
Output:
Example #2
Code:
class Hello{
public String name = "Abc";
public void SayHello(){
System.out.println("Hello World!");
}
}
class HelloMale extends Hello{
public void SayHello(){
System.out.println("Hello Mr. "+name+"!");
}
}
class HelloFemale extends Hello{
public void SayHello(){
System.out.println("Hello Ms. "+name+"!");
}
}
class Main {
public static void main(String args[]){
new Hello().SayHello();
new HelloMale().SayHello();
new HelloFemale().SayHello();
}
}
Output:
Business Use Case Example
Let’s say you need to implement a payment application for a business that accepts all types of cards, net banking, payment wallets, etc. Now, you have two ways to implement this:
- Define different names for each payment type viz a viz Payment_Card(), Payment_Netbanking(), Payment_Wallets()and so on.
- Overload a method name with different signatures – Payment(card_number, cvv), Payment(netbanking_id, auth_token), Payment(wallet_number)and so on.
Isn’t the second-way cleaner? This is how payment methods are overloaded in real-world scenarios.
Let’s take a step further to understand overriding. Now there are various card service providers such as VISA, MasterCard, AmericanExpress, Rupay, etc. Assume all of them use a common payment gateway except Rupay, which has a payment gateway of its own. Now you would write a parent class that contains the method definition for accepting payments via the common payment gateway. A child class for Rupay would inherit all the parent class features and override just one feature -the payment gateway method. After all, the child class differs only in the payment gateway method. All other features, such as card number, security token, etc., are the same.
Conclusion
Thus, we have learned the basic difference between the two most elementary object-oriented programming concepts, Overloading and Overriding. We have written a basic example and understood a real-life business scenario where overloading and overriding can be implemented. Both the concepts are easy to understand theoretically but take practice to implement in practical situations. It is, therefore, highly recommended to make it a habit of using overloading and overriding as much as possible in your programs. This will greatly help you in getting the hang of it.
Recommended Articles
This is a guide to the Difference Between Method Overloading and Method Overriding. Here we discuss the top key Difference Between Overloading and Overriding with infographics and examples. You may also have a look at the following articles to learn more –