Java is an object-oriented programming language that allows the implementation of OOPS concepts such as classes, objects, abstraction, polymorphism, inheritance, etc. Java is very popular because of its features of Java such as Method Overloading and Method Overriding which are very useful in our programming. Method overloading is implemented to overload the methods with the same name but different arguments. While method overriding is implemented to override the methods with the same name and same arguments such as child class overrides parent class. In this article, we’ll talk about the methods used for overloading and overriding. We’ll also discuss some key differences between these two.
First, let us understand what these two concepts are.
What is Method Overloading?
Method Overloading is used when we have two or more methods with the same name but the difference is only based on arguments or the types of arguments. Method overloading can be related to compile-time polymorphism as it applies the main idea behind polymorphism. Polymorphism means the same quality with different features.
For example, if you have to perform addition for the given numbers and there are a number of arguments that you pass in the function such as a(integer1, integer2) for two parameters and b(integer1, integer2, integer3,..) for more than 2 parameters. Then it becomes difficult to understand the behaviour of the method as the name is different but the method is the same.
To solve this problem, we use the method of overloading in Java. Another reason for its importance is that it increases the readability of the program.
Example:
class Sum{
static int add(int x, int y){
return x + y;
}
static int add(int x, int y, int z){
return x + y + z;
}
class TryMethodOverload{
public static void main(String[] args){
System.out.println(Sum.add(1,2));
System.out.println(Sum.add(1,2,3));
}}
Output:
3
6
Explanation: In the above code, we used two methods with the same name and we provided different arguments for these methods. Such that the methods are overloaded with different parameters when they have the same name.
Method overloading is very useful as it allows us to define functions with the same name multiple times. This helps in compile-time polymorphism.
What is Method Overriding?
Method Overriding in java is used when we implement more features in a subclass of the parent class. Such that the method will remain the same, but the subclass will override more features than the parent class. Method overriding is very useful as we don’t need to define a function again and again. Instead, we use the features of that function in another function with enhanced features.
Method overriding is used when we want to implement any specific method in an already implemented superclass. It is used for runtime polymorphism.
In method overriding, if the child class method calls the object at run time, then the methods of the parent class are automatically overridden by the child class.
Method overriding is used to implement runtime polymorphism. The access modifier in method overriding allows access to the overridden method only. But the static and private methods cannot be overridden in method overriding.
To call the parent class method, the super keyword is used.
Let us understand the concept of Method overriding with the help of an example below:
Example:
class Animal{
void run(){
System.out.println(“Animals are friendly.”);
}
class Dog extends Animal{
void run(){
System.out.println(“Dogs run very fast”);
}
}
class Cat extends Animal{
void run(){
System.out.println(“Cats run very quietly”);
}
}
public class MethodOverride{
public static void main(String[] args){
Dog barks = new Dog();
barks.run();
Cat meow = new Cat();
meow.run();
}
}
Output:
Dogs run very fast
Cats run very quietly
Explanation: In the above program, the parent class Animal is inherited by the child classes Dog and Cat. So that all the features of the parent class can be used by these child classes. Also, the additional features such as Dog barks, and Cat meows are the objects that override the parent class.
Method Overloading VS Method Overriding
In this section, we’ll see some key differences between Method overloading and Method overriding in java:
1. Method overloading takes place at compile-time of the program whereas method overriding happens at runtime of the program execution.
2. Overloading can be done for the same class but overriding needs base and child class for execution.
3. Overloading is faster than overriding because overridden methods are done at the runtime of the program.
4. Argument list can be different in overloading whereas it can be the same for method overriding.
5. For method overloading, static binding is used but for method overriding, dynamic binding is possible.
Differences between method overloading and method overriding are as follows:
Method Overloading | Method Overriding |
1. It increases the readability of the program. | 1. It allows a specific implementation to any parent class. |
2. The arguments passed in method overloading should be different. | 2. The arguments in method overriding can be the same. |
3. It happens at compile time of the program. | 3. It happens at the run time of the program. |
4. Method overriding can perform in the same class. | 4. Method overriding needs multiple classes to happen. |
5. Static binding is used in method overloading. | 5. Dynamic binding is used in method overriding. |
6. The performance of method overloading is faster than method overriding. | 6. The performance is slow because it happens at run time. |
7. The return type can be different in case of method overloading. | 7. In case of method overriding, the return type must be the same. |
8. The private and final methods are overloaded in method overloading. | 8. In method overriding, private and final methods can’t be overridden. |
9. Inheritance can be used in method overloading. | 9. Inheritance must be used in method overriding. |
10. Method overloading has the same names but different parameters. | 10. It has the same name, methods, and arguments. |
Conclusion:
Now we have concluded that both the methods are useful whenever they are needed. We discussed Method overloading and method overriding in detail in this blog. We have also seen the differences between these two methods that gave us a proper comparison. The involvement of class, objects, polymorphism, inheritance, etc. in these methods makes more clear the OOPS concepts.