Friday 5 September 2014

OverLoading vs Overriding



Method overloading and method overriding in Java is two important concept in Java which allows Java programmer to declare method with same name but different behaviour. Method overloading and method overriding is based on polymorphism in Java. 

In case of method overloading, method with same name co-exists in same class but they must have different method signature, while in case of method overriding, method with same name is declared in derived class or sub class.Method overloading is resolved using static binding in Java at compile time while method overriding is resolved using dynamic binding in Java at runtime. In short When you overload a method in Java its method signature got changed while in case of overriding method signature remains same but a method can only be overridden in sub class

OverLoad:
If you have two methods with same name in one Java class with different method signature than its called overloaded method in Java. Generally overloaded method in Java has different set of arguments to perform something based on different number of input. You can also overload constructor in Java, which we will see in following example of method overloading in Java. Binding of overloading method occurs during compile time and overloaded calls resolved using static binding. To overload a Java method just changes its signature. Just remember in order to change signature you either need to change number of argument, type of argument or order of argument in Java if they are of different types. Since return type is not part of method signature simply changing return type will result in duplicate method and you will get compile time error in Java.

 Remember you can overload static method in Java, you can also overload private and final method in Java but you can not override them.

You can overload the main() method, but only public static void main(String[] args) will be used when your class is launched by the JVM.


Override:
In order to override a Java method, you need to create a child class which extends parent. Overridden method in Java also shares same name as original method in Java but can only be overridden in sub class. Original method has to be defined inside interface or base class, which can be abstract as well. When you override a method in Java its signature remains exactly same including return type. JVM resolves correct overridden method based upon object at run-time by using dynamic binding in Java.

Rule of Method Override
  1. Method signature must be same including return type, number of method parameters, type of parameters and order of parameters
  2. Overriding method can not throw higher Exception than original or overridden method. means if original method throws IOException than overriding method can not throw super class of IOException e.g. Exception but it can throw any sub class of IOException or simply does not throw any Exception. This rule only applies to checked Exception in Java, overridden method is free to throw any unchecked Exception.
  3. Overriding method can not reduce accessibility of overridden method , means if original or overridden method is public than overriding method can not make it protected.

Summary
  1. First and most important difference between method overloading and overriding is that, In case of method overloading in Java, Signature of method changes while in case of method overriding it remain same.
  2.  Second major difference between method overloading vs overriding in Java is that You can overload method in one class but overriding can only be done on subclass.
  3. You can not override static, final and private method in Java but you can overload static, final or private method in Java.
  4. Overloaded method in Java is bonded by static binding and overridden methods are subject to dynamic binding.

No comments:

Post a Comment