Thursday, September 13, 2012

Rules of Method Overloading and Overriding in Java

Overloading and overriding rule in Java
Since you can either overload or override methods in Java, its important to know what are the rules of overloading and overriding in Java. any overloaded method or overridden method must follow rules of method overloading and method overriding to avoid compile time error and logical error, where you intend to override a method, instead method got overloaded. That is not uncommon and happen several times when Java programmer tries to override equals in Java or  overriding compareTo method in Java while implementing Comparable interface, which accepts Object type of argument. From Java 5 onwards which introduces @Override annotation along with Enum, Generics and varargs method you can completely avoid that problem. Anyway, let's see rules of method overloading and rule of method overriding in Java.

Rule of overloading a method in Java
Here is the list of rule which needs to be followed to overload a method in Java :

Rules of method overloading and overriding in Java1) First and foremost rule to overload a method in Java is to change method signature. method signature is made of number of arguments, type of arguments and order of arguments if they are of different types.

2) return type of method is not part of method signature, so just changing the return type will not overload method in Java.  see What is method overloading in Java for code example of these rules,

Overriding Rules in Java
Overriding is completely different than overloading and so it's rules are also different. For terminology, original method is known as overridden method and new method is known as overriding method. Following rules must be followed to correctly override a method in Java :

1) A method can only be written in Sub class, not in same class.

2) overriding method can not throw checked Exception which is higher in hierarchy, than checked
Exception thrown by overridden method. For example if overridden method throws IOException or ClassNotfoundException, which are checked Exception, than overriding method can not throw java.lang.Exception because it comes higher in type hierarchy (it's super class of IOException and ClassNotFoundExcepiton).

3) overriding method can not reduce access of overridden method. It means if overridden method is defined as
public than overriding method can not be protected or package private.

4) overriding method can increase access of
overridden method. this is opposite of earlier rule, according to this if overridden method is declared as protected than overriding method can be protected or public.

5) private, static and final method can  not be overridden in Java.

That’s all on Rules of method overloading and overriding in Java, Its very important to remember these rule to correctly overload and override any method in Java. Also remember to use @Override annotation to accidentally overloading a method, instead of overriding it.

Other Java tutorial from this blog

1 comment:

  1. plz upload clear information of method signature


Java67 Headline Animator