Thursday, November 13, 2008

Golden rules for Overriding

This blog entry sums up "all-you-ever-need-to-know" about Overridding Methods in Java. So just follow these simple rules and you will be home. :-)

Golden Rules:
  • The overridden method must have same number and type of arguments(i.e. the same signature) as in the original method of the superclass. Very simple to understand, so go ahead. :-)
  • Methods marked as final or static cannot be overridden. Although, a superclass and subclass can have static methods with same signatures, but that doesn't mean that you have overridden the method of superclass, rather these are inherently separate methods.
  • The overriding method CAN have less restrictive access modifier than the overridden method.
For Example, the following code would compile successfully
>   class base
>   {
>        protected void test()
      {
>          System.out.println(" Base");
>       }
>   }
>  class derived extends base
>  {
>          public void test()
>      {
         System.out.println(" Derived ");
>      }
>  }
  • The overriding method CANNOT have more restrictive access modifier than that of the original method of the superclass.
SuperClass Signature - protected void test(){ ... }
SubClass Signature     - private void test(){ ... }
This won't work. 
  • The overriding method CAN throw any unchecked exception regardless of the overridden method.
> class base
> {
> void hello()
> {
> System.out.println("base");
> }
>  }
> class derived extends base
> {
> void hello() throws RuntimeException
> {
> System.out.println("derived");
> throw new RuntimeException("My Exception from Derived");
> }
> }

  • The overriding method CANNOT throw any broader or new checked exceptions as compared to those thrown by overridden method. However, the overriding method CAN throw any narrower or fewer exceptions.
For Example, this is allowed:
SuperClass Signature - void disp() throws Exception { ... }
SubClass Signature     - void disp() throws RuntimeException { ... }

while this isn't:
SuperClass Signature - void disp() throws ArithmeticException { ... }
SubClass Signature     - void disp() throws RuntimeException { ... }
  • You can override only those methods that you can inherit. So, beware of code which seem to override private methods. 
  • The overridding method return type must be same as that of overridding method or it can be subclass of the original return type. 
> public class overriding
> {
> public static void main(String[] args)
> {
> derived d = new derived();
> derived e = d.print();
> e.print();
> }
> }
> class base
> {
> public base print()
> {
> System.out.println(" Inside Base ");
> return new base();
> }
> }
> class derived extends base
> {
>          @Override
> public derived print()
> {
> System.out.println(" Inside derived ");
> return new derived();
> }
> }

Here, the print() method is overridden though it doesn't look like that. This is known as Covariant-return type and it will work with code written for Java5 platform and above.

For any comments, suggestions or corrections, please leave your comments. :-)

No comments: