Method overloading and method overriding are important topics in Java, Difference between method overloading and method overriding is given below.
Property | Method Overloading | Method Overriding |
---|---|---|
Method name | Must be same | Must be same |
Argument type | Must be different(At least in order) | Must be same(Including Order) |
Method Signature | Must be different | Must be same |
Return Type | No restrictions | Must be the same until Java 1.4v but from Java 1.5v onwards co-variant return type allowed |
private,static, final method | Can be overloaded | can be overrided |
Access Modifier | No restrictions | The scope of access modifier cannot be reduced but we can increase |
throw clause | No restrictions | If the child class method throws any exceptions then the parent class method must throw the same checked exception as is parent but there are no restrictions for unchecked exception |
Method resolution | Always take care of the compiler based on the reference type | Always take care by compiler based on reference type |
known as | Compile Time Polymorphism/ early binding/ static polymorphism | Runtime Polymorphism/Dynamic Polymorphism/Late binding |
Note-
In overloading, we have to check only the method name(must be the same) and argument type (must be different). We don’t need to check any other thing like return type, access modifier.
But in overriding we have to check everything like method name, argument type, access modifier, return type, throws class, etc.
Consider the following method in parent class
public void method(int x)throws IOException
Now in the child class which of the following method we can take
public void method(int i) //Valid
public static void method(long i) //Valid
public static void method(int i) //Invalid
public void method(int i)throws Exception //Invalid
public static abstract void method(double i) //Invalid
Similar Java Tutorials
- Odd Even in Java
- Import statement in Java
- Abstract modifier in Java
- Strictfp modifier in Java
- Final variable in Java
- Static modifier in Java
- Native modifier in Java
- Transient keyword in Java
- Adapter Class in Java
- Method signature in Java
- Method overloading in Java
- Has A Relationship
- Inheritance in Java
- Encapsulation in Java
- Abstraction in Java
- Data Hiding in Java
- Interface vs abstract class
- Method Overriding in Java
- Method Overloading in Java