What is the Difference between Overloading and Overriding in Java

Introduction

In the previous tutorial, we have discussed the concept of inheritance in OOP along with the types of inheritance. Before you learn the concept of overloading and overriding you must know about inheritance in object oriented programming.

Overloading and Overriding

Method Overloading and Overriding are two vital concepts of Object Oriented Programming in Java. These concepts fall under the category of polymorphism. As polymorphism is the way to interpret the same message differently. You have different methods with the same names to interpret some message.

Function/Method Overloading in Java:

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

Function/Method Overriding in Java:

Overriding means two methods with same name and parameters. One of the methods is in the super class, and the other is in the subclass. Overriding allows a subclass to provide a specific implementation of a method that is already provided by its super class.

Understanding Overloading and Overriding

Overloading:

overloading in java
overloading in Java

Overloading Details

  1. Overloading happens at compile-time. The binding of overloaded method occurs at compile-time, also known as early binding.
  2. Static methods can be overloaded which means a class can have more than one static method of the same name.
  3. The most basic difference is that overloading is being done in the same class.
  4. Static binding is used for overloaded methods.
  5. Performance: Overloading gives better performance compared to overriding. The reason is that the binding of overridden methods is being done at runtime.
  6. Private and final methods can be overloaded. It means a class can have more than one private/final methods of the same name, but a child class cannot override the private/final methods of their base class.
  7. Method overloading is the example of compile time polymorphism.
  8. The return type of method does not matter in the case of method overloading; it can be same or different. However in the case of method overriding the overriding method should have a more specific return type.
  9. Argument list should be different while doing method overloading.

Overloading Example

Output:

java overloading output
java overloading output

OverRiding:

overriding in java
overriding in java

Overriding Details

  1. Overriding happens at runtime. Binding of overridden method call to its definition occurs at runtime, also known as late binding.
  2. You cannot override static methods, even if you declare a same static method in the child class, it has nothing to do with the same method name of the parent class.
  3. Method overriding is the example of run-time polymorphism.
  4. While for the overriding, base child class is required. Overriding is all about giving a specific implementation to the inherited method of the parent class.
  5. Dynamic binding is being used for overridden methods.
  6. In the case of method overriding the overridden method should have a more specific return type.
  7. Argument list should be same in Method Overriding.

Overriding Example:

Output

 

java overriding output
Java overriding output

 

Most People ask a question,

Why?

Why is overloading a compile time mechanism and overriding is the runtime mechanism?

The answer is so simple, in the case of method overloading, the compiler knows which method will be called on the basis of the instance/object, as only the names of the methods are same. They are two different methods with a single property in common. Everything else is different.

Whereas,

In the case of method overriding the compiler don’t know which method will be called as it depends on the instance/object whether it is of derived class or base class. Because each and every property of the two methods is same but the classes are different.

To further realize the situation, consider the example below.
If we have two classes

and

and our object is

so the compile time instance is of BaseClass but at runtime, DerivedClass method is called on the basis of the constructor used for the instance/object.

 

Hope you like this tutorial, Stay tuned for the upcoming tutorials. Stay Blessed!

In the next tutorial, you will learn about the concepts of abstract class and interface.