Difference Between Method Overloading And Method Overriding

4/5 - (3 votes)

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 OverloadingMethod 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.
Subscribe to Our LinkedIn Newsletter

Method Overloading | Compile-time Polymorphism

method overloading

In the above program, demo() method is overloaded with two different parameter lists.

Features of Method OverloadingDescription
Same Method NameIn method overloading, methods must have the same name.
Different ParametersMethods should have different parameters, either different types or different number of parameters.
Compile-Time PolymorphismMethod overloading is an example of compile-time polymorphism. The compiler determines which method to call based on the method signatures.
Same or Different Return TypeMethods 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.
ScopeMethod overloading can occur within the same class or in a subclass.
Static MethodsStatic methods can be overloaded, which means that if a class has more than one static method of the same name, they are overloaded methods.
ConstructorsIn Java, constructors can also be overloaded to create new objects in different ways.

Method Overriding | Run-time Polymorphism

method overriding

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 OverridingDescription
Same Method NameIn method overriding, the method in the subclass must have the same name as the one in the superclass.
Same ParametersThe method must have the same parameter list as the one in the superclass.
Runtime PolymorphismMethod overriding is an example of runtime polymorphism. The JVM determines which method to call at runtime, not at compile time.
Subclass OnlyMethod overriding can only occur in the subclass, not within the same class.
Same Return TypeThe return type must be the same or a subclass of the return type in the superclass’s method.
Access ModifiersThe access level cannot be more restrictive than the overridden method’s. For example, a public method cannot be overridden as private.
Final MethodsFinal 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 MethodsStatic 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:

google-news
Avinash

Avinash is the Founder of Software Testing Sapiens. He is a blogger and Software Tester who has been helping people to get thier Jobs over a years Now.

Leave a Comment

whatsapp-icon
0 Shares
Copy link