Monday 1 October 2012

Rules for Method Overriding in JAVA

Rules for Overriding Methods in Java


• Overridden method and argument list shall exactly be the same
• Return type shall be the subtype that is being declared in original method that is overridden in superclass, or it shall be the same.
• Restriction is not more in case of access level as compared to access level of the overridden method. E.g, when superclass is considerd to be public, in that case overriding method present in the subclass would not be public or private. But access level might not be much restrictive in comparison with access level of overridden method.
• If instance methods are inherited, by subclass, in that case only they might be overridden.
• Method which has been declared final might not get overridden.
• Method which has been declared static might be redeclared or not overridden
• Methods are not overridden if they are not inherited.
• Sublcass present in similar kind of package as that of instance superclass might be overridden to any method of superclass that has not been declared final or private.
• Subclass present in some other different package might override those methods which are declared protected or private.
• It is not possible to override constructors.

Rule 1) A method is said to be overriden if a class which extends another class defines method with the same name and arguments list.

Rule 2) The method defined in the base class should be visible in the derived class. If this is not so, the method in the derived class will not be considered overridden version but will be treated as a normal method.

Rule 3) The method name and arguments list should be same for overriding and overridden methods. But the return type can be co-variant. This means that if the return type of the method in super class is
Map, then the return type of the same method can be HashMap.

Rule 4) The access specifier in the overriding method (in the derived class) should not be more limiting than that of the overriden method (in the base class). This means that if the access specifier for base class method is protected then the access specifier for the derived class method should not be default or private but can be protected, public. The order of increasing visibility of various specifiers is:
private, default, protected, public

Rule 5) The exceptions specified in the derived class method should be either same or sub-class of them. Thus if the base class method specifies the exception as IOException in the throws clause then the derived class method can specify the exception as FileNotFoundException, IOException but not Exception.

Tips:
Tip 1) Choose to override when the base class/interface version is generic in nature.
Tip 2) Use inheritance and overriding of methods judiciously as it increases coupling between classes.
Tip 3) Try to invoke overridden methods by creating a reference of base class/interface type and making it refer to the derived class object. This helps in coding to generalization and helps creating lesser number of references.

No comments:

Post a Comment