Method Overloading vs Overriding in Java

This article is solely dedicated to clear the concepts of overriding Vs overloading in Java. These concepts are often confused and it results in a lot of discrepancies among developers. In order to become a successful programmer, you have to master the concepts and understand the key differences between them. We know that these two concepts sound very similar but they are actually different in their implementations.

Let us dive in and learn Method Overloading vs Overriding in Java.

Method Overloading vs overriding in java

Stay updated with latest technology trends
Join DataFlair on Telegram!!

Method Overloading in Java

Method Overloading in Java is the process of having different function implementations with the same function name. You might think of this as a function that behaves differently when different inputs pass through it. You use method overloading every day. Let us look at an example.

Let us say that your brain has a function called talk() which allows you to communicate with people around you. When you decide to talk to your friends, your body language is friendly and you crack jokes and mimic each other whilst having fun. But when you are talking with your parents, your behavior changes to being more respectful. You lower your tone and speak gently.

Notice that your brain is executing the same function talk() but, the behavior of the function changes based on the people you are talking to. This is method overloading in programming terms.

Now that you have understood the concept let us dive into the technicalities with a beautiful example. Shall we? In java, if the function has the same name but different parameters, then it is function overloading.

Why Method Overloading?

Method overloading simply gives us the liberty of defining multiple functions with the same name in a program. This allows the code to be simple and efficient because the method calls are resolved during runtime.

This takes into account the fact that the same operation may have different implementations based on the parameters of the function. For example, adding two numbers means mathematical addition but adding two strings means concatenating them together.

Java program to illustrate the use of method overloading:

package com.dataflair.overloadvsoverride;
public class OverloadExample {
  public static int area(int side) {
    //calculates and returns the area of square
    return side * side;
  }
  public static int area(int side1, int side2) {
    //calculates and returns the area of rectangle
    return side1 * side2;
  }

  public static void main(String[] args) {
    System.out.println(area(5));
    System.out.println(area(5, 2));
  }
}

Output:

25
10

Notice that the compiler executes the functions based on the number and type of parameters each of the functions has in their definition.

Now that we understand what overloading is, let us take a look at overriding in Java.

Rules of Method Overloading in Java

a. You cannot overload a return type in Java
b. The arguments to a function have to be different even when we overload static or final methods.
c. You can also overload the main method in Java.

Method Overriding in Java

Overriding is simply redefining a function of the parent class in the child class. In layman’s terms, if there is a child class that inherits a method from a parent class, the child class can have a different implementation of the same method! This process of redefining the method in the child class is known as Method overriding.

A very good real-life example would be you learning to bowl from your father but you decide to be a spinner instead of being a fast bowler like your dad. You just think that you will get more wickets that way.

So you override the method which you learned from your father and implement it in your own way. That is method overriding. Let us see an example that will help us grasp the topic even better!

Program to illustrate the use of method overriding in java:

package com.dataflair.overloadvsoverride;
class Father {
  void bowl() {
    System.out.println("Fast Bowler");
  }
}
class Child extends Father {
  void bowl() {
    System.out.println("Spin Bowler!");
  }
}

public class OverridingExample {
  public static void main(String[] args) {
    Father f = new Father();
    f.bowl();
    Father c = new Child();
    c.bowl();
  }

}

Output:

Fast Bowler
Spin Bowler!

Notice how we created an instance of the child class which had a modified definition of the “bowl” function! This is method overriding.

Rules of Java Method Overriding

a. You cannot override a final method.
b. You can only override a method that has the same or stronger access than the current method.
c. You cannot override a static method.
d. You cannot override a constructor.
e. The return type of an overridden method must be the same.
f. You cannot override a private method. ‘

Method Overriding Vs Method Overloading

1. Overloading occurs during compile time, whereas overriding occurs during runtime. In simple words, the compiler knows which method to execute in method overloading. In the example above, the compiler knows which method to execute as soon as the compiler compiles the program. This is static binding.

2. Overriding occurs during runtime, i.e, the compiler does not know what method to execute during compilation. It is executed during runtime. This is dynamic binding.

3. Overloading occurs within the class itself, whereas overriding requires inheritance between classes.

4. You can overload static functions in a class. This essentially means that you can have two or more static methods with the same name but different parameters.

5. Overriding static methods is not possible because of dynamic typing.

6. Overloading is more efficient than Overriding due to the fact that the compiler resolves the methods during runtime.

7. You can overload final or private methods in Java but you cannot override them. This basically means that you can have one or more final/private methods in the same class. But you cannot redefine a final or private method in a child class.

8. The return type for overloaded methods can be of any type. However, there are certain limitations to the return type of overridden methods. Check out the previous articles on method overriding to know more.

9. The argument list, or the parameter list, should be different while implementing function overloading. However, the parameter list should be the same while implementing method overriding.

Difference between Method Overloading and Method Overriding

Method OverloadingMethod Overriding
Enhances readability of the programRedefines a method which is already present in the parent class.
All operations involve only one class.Overriding involves inheritance between multiple classes. 
Classic example of compile type polymorphism(Static binding)Classic example of Run-time polymorphism(Dynamic Binding)
The parameters of the function must differ but the name should be the same.The parameters and the name should be the same.
Two overloaded functions can have different return types. The functions must have the same return type. 
One can overload static methodsOverriding static methods leads to method hiding and therefore not practiced. 

Summary

Here in this article, we came to know about the main differences between method overloading and overriding and when to use which. If you manage to understand these concepts nothing can stop you from writing efficient and clean code. You will also avoid redundant code blocks in the process.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.