EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Top Differences Tutorial Method Overloading and Method Overriding

Method Overloading and Method Overriding

Priya Pedamkar
Article byPriya Pedamkar

Updated March 27, 2023

Method Overloading and Method Overriding

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

Method-Overloading-and-Method-Overriding-info

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:

Difference Between Method Overloading and Method Overriding 1-1

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:

Example 1-2

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 –

  1. Method Overriding in Python
  2. Method Overloading in PHP
  3. Method Overloading in Python
  4. Function Overloading in Java
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Software Development Bundle5000+ Hours of HD Videos | 149 Learning Paths | 1050+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more