Introduction
Java Fundamentals
Java Flow Control
Java Arrays
Object Oriented Programming
Method Overloading & Overriding in Java
In this tutorial, we will learn about Method Overloading and Method Overriding in Java, two important concepts for every Java developer. We will also understand the differences between them and see examples to help us master these techniques.
1. Method Overloading:
Method overloading is the ability to define multiple methods with the same name in the same class. The methods must have different parameter lists. This means that the number of parameters, the type of parameters, or the order of parameters must be different. When a method is called, Java will determine which version of the method to use based on the number and type of the arguments passed in.
One important thing to keep in mind when overloading methods is that the return type of the methods does not matter. Two methods can have the same name, but different return types and still be considered as overloaded methods.
Method overloading can be used to create more flexible code by allowing a single method to perform different actions depending on the number or type of arguments passed in. This can make our code more readable and easier to maintain.
For example, consider the following class::
class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public String add(String a, String b) {
return a + b;
}
}
This class has three methods named add
, each with different parameter lists. The first method takes two integers
as arguments and returns their sum, while the second method takes two doubles
as arguments and returns their sum. The third method takes two strings
as arguments and concatenates them. When we call the add method, Java will determine which version of the method to use based on the number and type of the arguments passed in.
2. Method Overriding:
Method overriding is the ability to provide a different implementation of a method in a subclass that already exists in a superclass. When a method is overridden, the subclass's method will be called instead of the superclass's method when the method is invoked on an object of the subclass.
For method overriding, the method in the subclass must have the same method signature as the method in the superclass. The method signature includes the method name, the return type, and the parameter list. The method in the subclass must also have the same access level or a more restrictive access level than the method in the superclass.
It is important to use the @Override
annotation when overriding a method to ensure that the method we are trying to override actually exists in the superclass. This is to prevent accidental errors, such as misspelling the method name or providing the wrong parameter list.
For example, consider the following classes:
class Vehicle {
public void run() {
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle {
@Override
public void run() {
System.out.println("Bike is running");
}
}
In this example, the class Vehicle
has a method named run
that prints "Vehicle is running" when called. The class Bike
is a subclass of Vehicle and overrides the run method to print "Bike is running" when called. When we create an object of the Bike class and call the run method on it, the overridden method in the Bike class will be called and "Bike is running" will be printed.
3. Difference Between Method Overloading and Method Overriding:
Sr. | Method Overloading | Method Overriding |
---|---|---|
1 | Method overloading occurs within a single class | Method overriding occurs between a superclass and a subclass |
2 | Methods must have different parameter lists | Methods must have the same method signature, including the method name, return type and parameter list |
3 | Return type of the methods does not matter | Return type of the methods must be the same |
4 | Can be used to create more flexible code by allowing a single method to perform different actions depending on the number or type of arguments passed in | Can be used to provide a different implementation of a method in a subclass that already exists in a superclass. It is particularly useful when creating a class hierarchy. |
5 | Its not mandatory to use the same access level or a more restrictive access level | Method in the subclass must have the same access level or a more restrictive access level than the method in the superclass |
6 | It is also known as Compile-time polymorphism | It is also known as Run-time polymorphism |
It's important to notice that overloading a method can be done by just changing the number of arguments or the type of the arguments, while overloading a method requires to change the implementation of the method in the child class and to use the @Override
keyword.