Difference Between Overloading And Overriding

Difference Between Overloading And Overriding

5 mins read23.9K Views Comment
clickHere
Atul
Atul Harsha
Senior Manager Content
Updated on Aug 4, 2023 21:31 IST

Overloading and Overriding are two commonly used methods in programming. However, many people are perplexed whether these words are related in any way. In this article, we will look at the difference between Overloading and Overriding.

2022_09_Difference-Between-Overloading-And-Overriding.jpg

Programming involves many ideas like polymorphism, inheritance, data abstraction, and handling exceptions. Polymorphism, which includes ideas like method overloading and method overriding, is especially interesting to many people. To really understand polymorphism, it’s important to know the difference between overloading and overriding.

But, before understanding the difference between Overloading and Overriding, let’s quickly go over the table of content.

Table of Content

Difference between Overloading and Overriding

To understand the difference between Overloading and Overriding, in a better way, let’s go through the differences in a tabular format:

Parameter Overloading Overriding
Polymorphism type Compile-time Run-time
Usage Increases the readability of the program. Grants the specific implementation of the method already provided by its parent class or superclass.
Need of inheritance May or may not Always
Methods have Same name and different signatures Same name and same signature
Return type Can or can not be the same Must be the same or co-variant
Binding type Static Dynamic
Performance Good Poor
Overload private and final methods Yes No
Argument list should be Same Different

Difference between Overloading and Overriding – Explained with Analogy

Let’s understand the most important difference between Overloading and Overriding with an analogy of ordering a coffee at a coffee shop.

2023_08_iStock-1264377019.jpg

Overloading is like ordering a coffee at a coffee shop. You can order a “coffee” (method name), but you need to specify the type – an espresso, a latte, a cappuccino, etc. (parameters). They are all “coffee” but prepared differently based on your order.

Overriding, on the other hand, is like visiting a coffee shop that has a new way of making espresso. Maybe they use a different coffee bean or a new brewing method. When you order an “espresso” (method name) here, you get their version of espresso, not the usual one you get at other coffee shops. The new coffee shop has “overridden” the standard way of making espresso.

So, the main difference is that overloading is about using the same name (like “coffee”) for different things (like espresso, latte, cappuccino), while overriding is about changing the way something is done (like making espresso) in a new context (like a different coffee shop).

All You Need to Know About Proxy Servers
All You Need to Know About Proxy Servers
Have you heard the phrase “proxy server” but don’t know what it means? Nearly every day, folks use proxy servers to raise their privacy while browsing the internet significantly. But...read more
Difference Between XAMPP and WAMP
Difference Between XAMPP and WAMP
The article talks about XAMPP and WAMP servers, their pros and cons, and discusses the difference between XAMPP and WAMP.

What is Overloading?

Overloading definition: Overloading occurs when two or more methods in the same class have the same name but different parameters. 

You can also explore: Operator Overloading in C++

Method overloading is also called compile-time polymorphism, static polymorphism, or early binding. In Method overloading, the child argument takes precedence over the parent argument.

You can also explore: Difference Between Primary Memory and Secondary Memory

Example of Overloading:

 
// Java program to demonstrate working of method
// overloading in methods
class A {
    public int foo(int a) { return 20; }
  
    public char foo(int a, int b) { return 'a'; }
}
  
public class Main {
  
    public static void main(String args[])
    {
        A a = new A();
        System.out.println(a.foo(1));
        System.out.println(a.foo(1, 2));
    }
}
Copy code

Output:

20
a

Advantage of Overloading

Some of the advantages of overloading are:

  • Flexibility gets a boost: With overloading, the same method name can perform different tasks based on what it’s given.
  • Reading code is easier: You don’t need to remember different method names for similar tasks, thanks to overloading.
  • Different data types, no problem: Overloading allows a method to handle various data types, making your code versatile.
  • Efficiency is key: A single method name can perform different tasks based on input parameters, making your code more efficient with overloading.

You can also explore: Method Overloading in Java

What is Overriding?

Overriding definition: Method overriding is the act of declaring a method in a subclass that is already present in a parent class.

The main reason for having overridden methods in the child class is to have a different implementation for a method in the parent class.

You can also explore: Method Overriding in Java

Example of Overriding:

 
// Class Math
class Math {
    // method say which is overridden method here
   public void say() {
      System.out.println("This is class Math");
   }
}
// Class Topic
class Topic extends Math {
    // method say which is overriding method here
   public void say() {
      System.out.println("This is class Topics");
   }
}
class Main {
   public static void main(String args[]) {
      Math a = new Math(); // Math reference and object
      Math b = new Topic(); // Math reference but Topic object
      a.say(); // runs the method in Math class
      b.say(); // runs the method in Topic class
   }
}
Copy code

Output:

This is class Math
This is class Topics

Advantage of Overriding

Overriding is a powerful concept that makes your code more flexible, simple, efficient, and up-to-date.

  1. Think of it as a car upgrade: Overriding gives you all the features of the old model, plus some new ones.
  2. It’s as flexible as a Swiss Army knife: You can add or change tools as needed, while keeping the ones that are already useful.
  3. Imagine having a universal remote: Overriding simplifies things by letting you use a single set of controls for different devices.
  4. It’s like finding a shortcut on a road trip: Overriding makes your code more efficient, helping you get to your destination faster.

Some of the classic advantages of overriding are:

  1. Enhanced Functionality: Overriding allows a subclass to modify a method from its superclass, providing updated or improved functionality.
  2. Flexibility: Overriding enables a method to behave differently based on the class it’s in, offering flexibility in how methods perform.
  3. Code Simplification: Overriding uses the same method name across different classes, making the code easier to read and understand.
  4. Efficiency: Overriding can bypass unnecessary code in the superclass method, making the program run more efficiently.

You can also explore: Understanding Function Overriding in C++

Conclusion

When two or more methods in the same class have the same method name but different parameters, this is called overloading. In contrast, overriding occurs when two methods have the same name and parameters.

You can use Overloading and Overriding more effectively now that you understand the distinction between the two.

Recommended Reads

FAQs

Which is more effective, overriding or overloading?

Overloading outperforms overriding in terms of performance. The reason for this is that overridden methods are bound at runtime. Private and final methods can be overloaded, but not overwritten.

What is the primary distinction between overloading and overriding?

The main difference between overloading and overriding is that overloading occurs when methods in the same class have the same method name but different parameters, whereas overriding occurs when two methods have the same method name and parameters.

Is it possible to override static methods?

No. It is not possible to overload static methods because method overriding is based on dynamic binding at runtime, and static methods are bonded using static binding at compile time, we cannot override static methods.u00a0

Can overloading and overriding occur within the same class?

Yes, overloading and overriding can occur within the same class. Overloading happens when multiple methods have the same name but different parameters in a class, whereas overriding occurs when a subclass provides a different implementation for a method defined in its superclass.

Can an overloaded method have a different access modifier than the original method?

An overloaded method can have a different access modifier than the original method. Overloading is not concerned with the access modifiers of methods but rather with the method name and parameters.

Download this article as PDF to read offline

Download as PDF
clickHere
About the Author
author-image
Atul Harsha
Senior Manager Content

Experienced AI and Machine Learning content creator with a passion for using data to solve real-world challenges. I specialize in Python, SQL, NLP, and Data Visualization. My goal is to make data science engaging an... Read Full Bio

Comments

We use cookies to improve your experience. By continuing to browse the site, you agree to our Privacy Policy and Cookie Policy.