Last Updated: 25 December, 2023
Polymorphism is one of the strongest features of object-oriented programming. There are two types of polymorphism: compile-time polymorphism and run-time polymorphism.
Compile-time polymorphism can be achieved through method overloading, and runtime polymorphism can be achieved through method overriding.
In this tutorial, we will see what method overloading and method overriding are, with examples, and what the differences are between them.
In a Java class, writing two or more methods with the same method name but different parameters (which may be different in number of parameters or type of parameters, or both). There method are called overloaded methods and this feature is called method overloading.
Method overloading is an implementation approach for Compile-Time Polymorphism.
Overloaded methods must be declared within the same class.
The overloaded method must be different in the argument list (number of parameters, data type of parameters, or order of parameters).
The overloaded method can have the same or a different return type.
Overloaded methods can have different access modifiers (public, private, or protected).
Overloaded methods can throw different exceptions.
Here’s an example of creating an overloaded methods in Java.
Example 1: Method Overloading - By changing the number of parameters
In the above example, the class Addition has three overloaded add() methods, and each method has a different number of int parameters. These methods are called based on their number of parameters. See the output below.
Output
Added two int values: 60
Added three int values: 90
Added four int values: 220
Example 2: Method Overloading - By changing the data types of parameters
Here, the Addition class has three overloaded add methods, and each add method has two parameters, but each method's parameter types are different from each other, so the compiler will call these methods according to their data types at compile time.
Output
Added two int values: 60
Added two float values: 66.25
Added one int and one float value: 110.25
Run-time polymorphism can be achieved through method overriding in Java. Method overriding can be performed only in inheritance, which means we declare the same method in both parent and child classes with the same signature.
"Method overriding" is the process of redefining a method in a child class that is already defined in the parent class. When both parent and child classes have the same method, then that method is said to be the overriding method. The method overriding enables the child class to change the implementation of the method acquired from the parent class according to its requirements.
This polymorphism is resolved during runtime and is achieved through method overriding. It is also known as "dynamic method dispatch."
The overriding method must have the same name, parameter list, and return type as the overridden method.
The overriding method cannot be less accessible than the overridden method (e.g., can't be private if the overridden method is public).
Let's understand how to implement method overriding in Java with the help of the given example below.
Example: Method Overriding Implementation in Java
In the above example, the Square class is extending the Shape class and rewriting the draw() method. That means the draw() method is overridden in the child class (Square). When we call the draw() method, only the child class's draw() method is called.
Output
Square class - draw() method
Square class - draw() method
Now, let's see the differences between method overloading and method overriding in Java.
There are the following key differences between method overloading and overriding in Java:
Parameters | Method Overloading | Method Overriding |
---|---|---|
Class | Performs within the same class. | Perform between two classes through the Inheritance (IS-A relationship). |
Method Parameters | The Method parameters must be different. | The Method parameters must be the same. |
Return Type | The Return type may be the same or different. | The return type must be the same. |
Access Modifiers | Access modifiers may be the same or different. | The access modifier for a subclass method must be the same or higher than the access modifier of the superclass method. |
Method Signatures | Every method signature must be different (with the same name) in the case of Method Overloading. | Every method signature must be the same (with the same name) in the case of Method Overriding. |
Final/Static/Private Method | Method overload is possible. | Method overriding is not possible. |
Method Resolution | Handled by the Java compiler based on the reference type. | Handled by the JVM based on runtime objects. |
Performance | Provides better performance. | Provides lesser performance. |
Polymorphism | Also known as Early Binding, Static Polymorphism, or Compile-time Polymorphism. | Also known as Late Binding, Dynamic Polymorphism, or Runtime Polymorphism. |
Uses | Used to increase the readability of the program. | Used to provide the specific implementation of the method that is already provided by its superclass. |
In summary:
Use method overloading to provide multiple options for the same action within a class.
Use method overriding to create specialized behavior for subclasses while maintaining a common interface.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com