Difference between Method Overloading and Method Overriding in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Method overloading in Java allows multiple methods with the same name but different parameters in a class. In contrast, method overriding allows a subclass to provide a specific implementation of a method already defined in its superclass.

A list of more differences between method overloading and method overriding is given below:

BasisMethod OverloadingMethod Overriding
DefinitionMethod overloading is when two or more methods have the same name and different arguments.Method overriding is when a subclass modifies a method of a superclass having the same signature.
Method SignatureThe overloaded methods must have different method signatures. This means that the methods must differ in at least one of these: number of parameters, type of parameters, and order of parameters, but they should all have the same name.The overridden methods must have the same method signature. This means that the method name, number of arguments, order of arguments, and type of arguments should match exactly.
Return TypeThe return type of overloaded methods may or may not have the same return type.If the base class method has a primitive data type, then the overridden method of the subclass must have the same data type.But if the base class method has a derived return type, then the overridden method of the subclass must have either the same data type or subclass of the derived data type.
Access ModifierWe do not have any restriction on access modifiers and may use any access modifier in the overloaded method.The overridden method in subclass should have the same or less restricted access modifier.
BindingThe method call is binded to method definition at compile time. Also known as Static Binding.The method call is binded to method definition at compile time. Also known as Dynamic Binding.
Polymorphism TypeIt is an application of compile-time polymorphism.It is an application of run-time polymorphism.
ScopeThe overloading functions always exist in the same scope.The overriding function always exists in different scopes.
PurposeGives functionality to allow different methods to have the same name and act differently based on the parameters supplied to them.It gives functionality to add some extra or unexpected functionality in the subclass compared to the superclass.
Inheritance ScopeIt doesn't need inheritance, and it can be performed within the same class.Relies on inheritance, which occurs in classes depicting an is-a (parent-child) relationship.
Private MethodsCan be overloadedCannot be overridden
Final MethodsCan be overloadedCannot be overridden
Static MethodsCan be overloadedCannot be overridden
Error HandlingIf overloading breaks, we get a compile-time error, which is relatively easy to fix.If overriding breaks, we get run-time errors, which are more serious and relatively more complex to fix.
UsageWe can overload methods any number of times in a class.We can override a method only once in an inheriting class.
Object TypeCan be performed within a single class or involve static methods.Involves a superclass and its subclass.

Key Difference between Method Overloading and Method Overriding in Java

The main difference between method overloading and method overriding in Java is that:

Overloading occurs within a class when two or more methods have the same name but different parameters. This allows us to provide different implementations of the same method for different data types or for different numbers of arguments. Overloading is a compile-time polymorphism.

In contrast, Overriding occurs between a subclass and its superclass when a subclass implements a method already defined in the superclass. This allows us to specialize the method's behavior for the subclass. Overriding is a runtime polymorphism.

Method Overloading in Java

Method overloading in Java refers to the concept where more than one method shares the same method name and has a different parameters list in the same class. Using method overloading, we can define methods that do similar tasks under the same name.

Method overloading is an example of compile-time polymorphism, static or early binding. Compile-time polymorphism is when we bind an object with its functionality at compile-time only. In Java, the JVM knows before executing the code which method or object it will execute for a particular call.

Example of Method Overloading in Java

If we run the above code, we’ll get the following output:

Output:

Explanation:

As we can see, we have called add() with different combinations of parameters, and each time the JVM automatically calls the required method. This is how we can do method overloading.

Also if we are using an IDE and do “ctrl+click” on any of add() methods in the main() function, we’ll see that it takes us to the function it is going to call while executing. This shows that before running our code, JVM already knows which method to execute for a call. This is why it is also called compile-time polymorphism.

Method Overriding in Java

Method overriding is simply redefining a function of a superclass in the subclass with the same method signature. The same signature means the methods must have the same name, the same number of parameters, and the same type of parameters in the same sequence. It is used to change existing methods' behaviour and provide fine-grained implementations in subclasses for methods defined in a superclass.

Example of Method Overriding in Java

Output:

Explanation:

  1. The Train class is defined with three methods: accelerate(), start(), and stop(). Each method returns a string representing the action performed.
  2. The BulletTrain class extends the Train class and overrides all three methods using the @Override annotation. The overridden methods provide specialized implementations for the BulletTrain class.
  3. The Example class contains the main() method, where instances of both Train and BulletTrain are created and used to invoke the overridden methods.
  4. In the main() method, a Train object is created, and its start(), accelerate(), and stop() methods are called. The output of these method invocations is printed.
  5. Next, a BulletTrain object is created and assigned to a Train reference. This demonstrates polymorphism, as a subclass object can be assigned to a superclass reference.
  6. The overridden methods in the BulletTrain class are invoked using the bulletTrain reference. The output of these method invocations is also printed.

Conclusion

  • Method Overloading and Method Overriding are two important concepts in object-oriented programming. They allow programmers to write more flexible and reusable code.

  • Method overloading allows us to define multiple methods with the same name but different parameters. This is useful when we want to perform the same operation on different types of data or with different numbers of arguments.

  • Method overriding allows us to redefine a method in a subclass that is already defined in its superclass. This is useful when we want to customize the behaviour of a method for a specific subclass.

  • Benefits of method overloading and method overriding:

    • Code reuse: Allow us to reuse code by reducing the amount of duplicate code that we have to write.
    • Flexibility: Make our code more flexible by allowing us to write methods that can be used in different ways.
    • Maintainability: Make our code more maintainable by making it easier to understand and modify.