In this core java tutorial we will learn what are differences between Method overloading and Method overriding in java.
Method overloading and Method overriding forms base of core java. It’s very important to differentiate between two. Both have been used intensively in java Api’s. This is one the most prominently asked important interview question in java.
You must also know 10 Differences between Interface and abstract class in java - in detail with programs
Method overloading
|
Method overriding
| |
1
|
When a class have same method name with different argument, than it is called method overloading.
|
Method overriding - Method of superclass is overridden in subclass to provide more specific implementation.
|
2
|
Method is overloaded by - keeping same name of method and only changing number of arguments
Let’s compare with method overriding in java.
|
In Method overriding - Method of superclass is overridden in subclass when overriding method of subclass in java -
For more detail on this point please Read : Throw/declare checked and unchecked exception while overriding superclass method in java
|
3
|
Method overriding is always done in subClass in java.
| |
4
|
Only instance methods can be overridden in java.
Static methods can’t be overridden in java. (Please refer this article for detailed analysis and explanation with program)
| |
5
|
Main method can’t be overridden in java, because main is static method and static methods can’t be overridden in java (as mentioned in above point)
| |
6
|
private methods can be overloaded in java.
|
private methods can’t be overridden in java, because private methods are not inherited in subClass in java.
|
7
|
|
final methods can’t be overridden in java, because final methods are not inherited in subClass in java.
|
8
|
Call to overloaded method is bonded at compile time in java.
|
Call to overridden method is bonded at runtime in java.
|
9
|
Method overloading concept is also known as compile time polymorphism in java.
|
Method overriding concept is also known as runtime time polymorphism in java.
|
10
|
Example - Please see below
|
Example - Please see below
|
Now we will learn differences between Method overloading and Method overriding in java with program and examples.
Example of method overloading in java -
/*
* Method to calculate sum of 2 arguments
*/
public void sum(int x, int y) {
System.out.println("sum of 2 arguments = "+ (x+y));
}
/*
* Method to calculate sum of 3 arguments
*/
public void sum(int x, int y, int z) {
System.out.println("sum of 3 arguments = "+ (x+y+z));
}
|
sum() method logically perform almost similar tasks and the only difference is in number of arguments. Method overloading enables same method name sum() to be reused in program in java.
Example of method overriding in java-
Different animals eat different food, like Lion eat flesh and Goat eat grass. So we can have generic SuperClass which tells that Animal might eat flesh, grass or may be some other thing.
Now, we can have SubClasses like Lion which more specifically that Lion eat flesh.
So at runtime, rather than calling food() method of SuperClass, food() method of subclass will be called and this way we could derive advantage of creating more specific SubClasses and overriding method. We will elaborate this in Program 1.
/*
* superclass - Animal
*/
class Animals {
void food() {
System.out.println("Animal may eat flesh, grass or ....");
}
}
/*
* subclass of Animal - Lion
*/
class Lion extends Animals {
@Override
void food() {
System.out.println("Lion eat - flesh");
}
}
|
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
Summary >
So in this core java tutorial we learned what are 10 important differences between Method overloading and Method overriding in java.
RELATED LINKS>
Method overloading in java - in detail with programs,10 Features,need of method overloading, overloading main method, Diagram and tabular form of Implicit casting/promotion of primitive Data type in java
Method overriding in java - in detail with programs, 10 Features, need of method overriding, understanding @Override annotation, Covariant return, diagram to understand access modifiers in java
Difference between String, StringBuffer and StringBuilder in java - In depth coverage in java
Constructor in java - Constructor chaining, access modifiers with constructors, constructor overloading, exception thrown, constructors are not inherited
Labels:
Core Java
Core Java Differences