Java Overriding vs Overloading
Method overloading, is when method with same name co-exists in same class but they must have different method signature.
- Method overloading is done via static binding.
Method overriding, is when method with same name is declared in derived class or sub class.
- Method signature must be same including return type, number of method parameters, type of parameters and order of parameters
- Overriding method can not throw higher Exception than original or overridden method. This rule only applies to checked Exception in Java, overridden method is free to throw any unchecked Exception.
- Overriding method can not reduce accessibility of overridden method i.e if original or overridden method is public than overriding method can not make it protected.
- Private and Final methods cannot be overridden.
- Method overriding is done via dynamic binding, therefore static methods cannot be overridden.
Comments