Explore the fundamental differences between method overloading and method overriding in Java with our complete comparison table. Understand key concepts like compile-time and runtime polymorphism, method parameters, return types, exception handling, and visibility modifiers to enhance your Java programming skills. Ideal for beginners and experts alike, this guide offers clear insights into these crucial OOPs concepts.
Method Overloading | Method Overriding |
---|---|
In method overloading, methods must have the same name but different parameters. | In method overriding, methods must have the same name and same parameters. |
It is a compile-time polymorphism. | It is a runtime polymorphism. |
It occurs within the same class. | It occurs between two classes that have IS-A (inheritance) relationship. |
It increases the readability of the program. | It provides the specific implementation of the method that is already provided by its superclass. |
Return type can be same or different. | Return type must be same or co-variant (return type may vary in the same direction as the subclass). |
Static methods can be overloaded. | Static methods cannot be overridden, they can be hidden. |
Constructors can be overloaded. | Constructors cannot be overridden. |
Exception handling does not matter in overloading. | Overridden method cannot throw a checked exception that is new or broader than the ones declared by the overridden method. |
Methods can be overloaded with different modifiers. | Overriding method cannot be less visible than overridden method. |
Method Overloading | Compile-time Polymorphism
Table of Contents
In the above program, demo()
method is overloaded with two different parameter lists.
Features of Method Overloading | Description |
---|---|
Same Method Name | In method overloading, methods must have the same name. |
Different Parameters | Methods should have different parameters, either different types or different number of parameters. |
Compile-Time Polymorphism | Method overloading is an example of compile-time polymorphism. The compiler determines which method to call based on the method signatures. |
Same or Different Return Type | Methods can have the same or different return type in method overloading. However, they cannot be overloaded by return type alone. Changing only the return type will lead to a compilation error. |
Scope | Method overloading can occur within the same class or in a subclass. |
Static Methods | Static methods can be overloaded, which means that if a class has more than one static method of the same name, they are overloaded methods. |
Constructors | In Java, constructors can also be overloaded to create new objects in different ways. |
Method Overriding | Run-time Polymorphism
In the above program, run()
method of class Bike is overriding the run()
method of class Vehicle. When you call obj.run()
, the run()
method of Bike class is called because obj is instance of Bike class.
Features of Method Overriding | Description |
---|---|
Same Method Name | In method overriding, the method in the subclass must have the same name as the one in the superclass. |
Same Parameters | The method must have the same parameter list as the one in the superclass. |
Runtime Polymorphism | Method overriding is an example of runtime polymorphism. The JVM determines which method to call at runtime, not at compile time. |
Subclass Only | Method overriding can only occur in the subclass, not within the same class. |
Same Return Type | The return type must be the same or a subclass of the return type in the superclass’s method. |
Access Modifiers | The access level cannot be more restrictive than the overridden method’s. For example, a public method cannot be overridden as private. |
Final Methods | Final methods cannot be overridden. This means that if you declare a method as final in the superclass, it cannot be modified in the subclass. |
Static Methods | Static methods cannot be overridden. If you declare a static method in the superclass and then declare a similar method in the subclass, it does not override the one in the superclass, it merely hides it. |
Recommended Posts: