Method Overloading
Tips
- parameter change (count/datatype) is involved in method overloading but if anything else is change keeping the method name, datatype and paramters same then it is considered as duplicate method.(even changing to static, changing access modifiers, changing return type)
- static methods can be overloaded
- we cannot change the return type in overloading, compile time error occurs
- null cannot be passed to overloaded methods with non primitive data type(Compile time error – example below)
- final methods can be overloaded
Rules of method overloading
- The number of parameters in two methods.
- The data types of the parameters of methods.
- The Order of the parameters of methods.
Below point does not affect method overloading
- return type
- access modifiers
- Check and unchecked exceptions
What happens when method signature is the same and the return type is different?
The compiler will give an error as the return value alone is not sufficient for the compiler to figure out which function it has to call. More details:Java Language Specification: §8.4.2.
Only if both methods have different parameter types (so, they have a different signature), then Method overloading is possible.
null cannot be passed to overloaded methods with non primitive data type(example below)
public class Test { // Overloaded methods public void fun(Integer i) { System.out.println("fun(Integer ) "); } public void fun(String name) { System.out.println("fun(String ) "); } // Driver code public static void main(String [] args) { Test mv = new Test(); // This line causes error mv.fun(null); } }
Compile time error occurs, but if a object is null and the object reference if passed into the method then no compile time error
Method Overriding
Rules of method overriding
- Only inherited methods can be overridden
- public and protected methods will always be overriden
- private methods cannot be overriden and they are new methods all together in child class
- default methods can only be overriden inside the same package or subpackage
- Final and static method cannot be overriden
- if final method is overridden compile time error is displayed in child class
- static methods are available to all the instances of super class and subclass hence it cannot be overriden. But a static method in the child class can be created
- Overriding methods should have the same argument list
- Else it becomes an alltogether new method in child class
- And such new methods below not work with runtime polymorphism Animal a = new Dog(); a.methodCall()
- The overriding method must have same return type (or subtype).
- The overriding method must not have more restrictive access modifier
- The overriding method must not throw new or broader checked exceptions
- Use the super keyword to invoke the overridden method from a subclass.
- Constructors cannot be overridden
- Abstract methods must be overridden by the first concrete (non-abstract) subclass
- A static method in a subclass may hide another static one in a superclass, and that’s called hiding.
Below points has no effect on overriding
- The synchronized modifier has no effect on the rules of overriding.
- The strictfp modifier has no effect on the rules of overriding.
Tips :
- Animal a = new Dog(); a.methodCall() //Runtime polymorphism
- In the above scenarion, methodCall() is expected to be executed of Dog class 1st if and only if the method is overriden else Animal method is called.
- If the method does not exist in the parent class then compile time error will be displayed
Example : (When same name method is dangerious, understand runtime polymorphism)
public class Dog extends Animal{ public static void main(String[] args) { Animal animal = new Dog(); animal.print(10); //Compile Time error } public void print(int x) { System.out.println("inside dog class"); } } class Animal{ public void print() { System.out.println("inside animal class"); } }
Reference :
- geeksforgeeks-overload
- codejava-override