In Java, What?s the Difference Between Method Overloading and Method Overriding?


method overloading and overriding

*This post may contain affiliate links. As an Amazon Associate we earn from qualifying purchases.

In Java, method overloading and overriding are frequently used to optimize code and its readability. This article walks you through the main differences between these two practices.

Comparison: Method Overloading and Overriding

Method overload is calling the same name method multiple times in the same class; these methods can have different parameters and return types. Method override, however, calls the same method in separate classes – parent and extended child class – and the overriding method must have a specific return type in relation to method in the parent class. Here’s how they differ in usage and performance:

Method Overloading

A programmer using method overload will declare the same method under one class. For example, he might have an add() method that can take a varying number of integers and return their sum or difference. Here’s what it would look like:

Class Sum{
int math(int num1, int num2, int num3)
{return num1+num2+num3; }

int math(int num1, int num2)
{return num1-num2; }

public static void main(String args[])
{
Sum obj = new Sum();
System.out.println(“The sum is: “+obj.math(3,4,2));
System.out.println(“The difference is: “+obj.math(10,3));
}
}

In the above example code, the math() method is overloaded, with different parameters and definitions to perform varying math operations. The methods are called within the same class Sum. Here’s the output:

The sum is: 9
The difference is: 7

Method Overriding Occurs in Different Classes

In method override, a programmer declares a method in a parent class and its extended class. Unlike method overloading, override requires the methods to have the same parameters and return types that are return-type-substitutable. Simplified, this means override method return types must be the same or covariant. Below is a sample code from beginnersbook that demonstrates how method override can be used:

class CarClass
{
public int speedLimit()
{return 100;}
}
class Ford extends CarClass
{
public int speedLimit()
{return 150;}
public static void main(String args[])
{
CarClass obj = new Ford();
int num= obj.speedLimit();
System.out.println(“Speed Limit is: “+num);
}
}

Output:
Speed Limit is: 150

In their example, method speedLimit() is called in the parent CarClass and its extended Ford class. At run time, following the declaration CarClass obj = new Ford(), the speedLimit() method in Ford class overrides the method in its parent class.

The argument and return type is the same for method override, whereas parameters and argument list must be different for method overload. Here’s a simpler example from Intel that used method override, where the return type is the same across the methods and neither are taking arguments. The overriding method differs only in the value that’s printed for a specific Dog class that’s extended from a more generic Animal class:

class Animal{
void eat(){System.out.println(“eating…”);}
}
class Dog extends Animal{
void eat(){System.out.println(“eating bread…”);}
}

Performance Differences between Method Overloading and Overriding

As previously mentioned, method override occurs at run time when the program executes its definition. Method overloading occurs when the program is compiled and therefore performs better than override.

Method overloading is typically used to enhance program readability and support different parameter types, whereas method override is meant to execute a specific implementation of a method. In the sample code above, method override was used to implement the speedLimit() method for the Ford instance.

Recommended read: If you really want to be prepared for your interview on Java, check out these Java interview questions and answers.

Conclusion

Method overload and method override are used in Java programming to improve how a program reads and runs. For example, method overload uses the same name method to perform similar functions, like various math operations with a different argument list. When assessing when to use method overloading and overriding, programmers should consider their difference and usage to decide how each can be applied to optimize their code.

Recent Posts