Suggested Pages

Thursday, May 3, 2012

Java Overriding

In this post we are going to show the main rules to know on Java Overriding.

Example-1 overriding does not work successfully if the return type of the overridden method and the return type of the overriding method are different primitive types


The following class does not compile successfully because the return type of method doThis of the Child class is not covariant with the return type of method doThis of the Parent class

package example;
public class Parent {
 double doThis(double d){}
}

 class Child extends Parent {
 float doThis(double d){} 
}

Example-2 overriding works successfully with covariant return types


The following class, compiles successfully because the return type of method doThis of Child class (String) is covariant with the return type of the method of Parent class (Object).

package example;

public class Parent {
  public Object doThis() {}
}

class Child extends Parent{
  public String doThis() {}
}

Example-3 overriding method can throw less exceptions than the overridden method, but not different exceptions


The following class compiles successfully because the method doThis of Parent class throws IOException, instead the method doThis of class Child doesn't throw any Exception
package example;
public class Parent {
void doThis() throws IOException {}
}

class Child extends Parent {
void doThis() {}
}

Example-4 overriding methods can throw any subclasses of the Exception thrown by the overridden methods


The following class compiles successfully because the overriding method of class Child is throwing a subclass exception (E1) of the exception thrown by the overridden method of class Parent (E).
 
package example;
public class Parent {
 void doThis() throws E {}
}

class Child extends Parent {
void doThis() throws E1{}
}

class E extends Exception {}
class E1 extens E{}
class E2 extends E{}

Example-5 primitive types are not subclasses of wrapper classes


The following class does not compile successfully because wrapper classes are not covariant with Java primitive types.
 
package example;
public class Parent {
public int doThis(){..}

}
class Child extends Parent {
  public Integer doThis(){..}
}

Example-6 private methods can't be overridden


The following class compiles successfully because the private method of Parent class is not visible to subclasses and then it can't be overridden by any subclasses. Therefore you can put a method in the subclass (Child) with the same method signature of doThis method of class Parent.
 

package example;

public class Parent {
   private void doThis() {...}
}

class Child extends Parent{
   public void doThis() {...}
}

Example-7 reduce the visibility of the overridden method is not allowed


The following class does not compile successfully because the method doThis of class Child reduces the visibility of overridden method.

package example;

public class Parent {
 public void doThis() {...}
}

class Child extends Parent{
 private void doThis() {...}
}

No comments :

Post a Comment

Suggested Pages