Overloading and Overriding in java
-
Overloading happens at compile-time while Overriding happens at runtime: The binding of overloaded method call to its definition has happens at compile-time however binding of overridden method call to its definition happens at runtime.
-
Static methods can be overloaded which means a class can have more than one static method of same name. Static methods cannot be overridden, even if you declare a same static method in child class it has nothing to do with the same method of parent class.
-
The most basic difference is that overloading is being done in the same class while for overriding base and child classes are required. Overriding is all about giving a specific implementation to the inherited method of parent class.
-
Static binding is being used for overloaded methods and dynamic binding is being used for overridden/overriding methods.
-
Performance: Overloading gives better performance compared to overriding. The reason is that the binding of overridden methods is being done at runtime.
-
private and final methods can be overloaded but they cannot be overridden. It means a class can have more than one private/final methods of same name but a child class cannot override the private/final methods of their base class.
-
Return type of method does not matter in case of method overloading, it can be same or different. However in case of method overriding the overriding method can have more specific return type (refer this).
-
Argument list should be different while doing method overloading. Argument list should be same in method Overriding.
Overloading and Overriding in java
No.
|
Method Overloading
|
Method Overriding
|
---|---|---|
1)
|
Method overloading is used to increase the readability of the program.
|
Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
|
2)
|
Method overloading is performed within class.
|
Method overriding occurs in two classes that have IS-A (inheritance) relationship.
|
3)
|
In case of method overloading, parameter must be different.
|
In case of method overriding, parameter must be same.
|
4)
|
Method overloading is the example of compile time polymorphism.
|
Method overriding is the example of run time polymorphism.
|
5)
|
In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.
|
Return type must be same or covariant in me
|
post a comment