Overriding

Overriding

What ever the parent has by default available to the child class through inheritance, If the child class is not satisfied with the parent class implementation then the child is allowed to overwrite that parent class method to provide it’s own specific implementation, this concept is nothing but “overriding”.

/**
 * 
 * @author ashok.mariyala
 *
 */
public class P {
   public void property() {
      System.out.println("Land, gold, cash");
   }
   public void mary() {
      System.out.println("Subbalakshmi");
   }
}
/**
 * 
 * @author ashok.mariyala
 *
 */
public class C extends P {
   public void mary(){
      System.out.println("Priyanka");
   }
}
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public static void main(String[] args) {
      P p1 = new P();
      p1.mary(); //Subbalakshmi
      C c1 = new C();
      c1.mary(); //Priyanka
      P p2 = new C();
      p2.mary(); //Priyanka
   }
}

Overriding method resolution will take care by JVM based on run time Object. Hence overriding is considered as run time polymorphism or dynamic polymorphism or late binding. The process of overriding method resolution is also known as “dynamic method dispatch”.

Rules for overriding
  • The method names and arguments(including order) must be same. i.e signatures of the methods must be same.
  • In overriding the return types must be same but this rule is applicable only until 1.4 version but from 1.5 version on wards co-variant return types also allowed. i.e the child class method return type need not to be same as parent class method return type it’s child class also allowed.

E.g

Parent : Object(covariant returntype).
Child : String, StringBuffer, Object….. // Fine

Parent : String
Child : Object  // C.E
Because String to Object is not a covariant.

Parent : double
Child : int // C.E
Because co-variant return types are not applicable for primitive types.
  • final method can’t be overridden in child classes. Private methods are not visible in the child classes. Hence they won’t participate in overriding. Based on our requirement we can take exactly same declaration in child class, But It is not overriding.
class P {
   private void m1() {
   }
}

class C extends P {
   private void m1() {
   }
}

Here it is not overriding child class specific method

  • native methods can be overridden as non-native, similarly we can override abstract methods, synchronized methods. We can override a non-abstract method as abstract also.
  • while overriding we are not allowed o decrease access privileges. Otherwise compile time error but we can increase access privileges.
  • while overriding the size of checked exceptions we are not allowed to increase in throws class but we can decrease the size of checked exceptions. But there are no restrictions on unchecked exceptions.
Overriding in static methods

A static method can’t be overridden as non-static

E.g

class P {
   public static void m1() {
   }
}

class C extends P {
   public void m1() {
   }
}
m1() in C cannot override m1() in P; overridden method is static

 Similarly a non-static method can’t be overridden as static method.

If both parent and child class methods are static then there is no compile time error or run time error it seems that overriding is happened but it is not overriding this concept is called “method hiding”. All the rules of method hiding are exactly similar to overriding, except both methods declared as static.

In the case of method hiding method resolution will take care by compiler based on reference type(But not runtime object).

class P {
   public static void m1() {
      System.out.println("parent method");
   }
}

class C extends P {
   public static void m1() {
      System.out.println("child method");
   }
}

class Test {
   public static void main(String arg[]) {
      P p = new P();
      p.m1(); //parent method
      C c = new C();
      c.m1(); //child method
      P p1 = new P();
      p1.m1(); //parent method
   }
}

In the case of method hiding the method resolution will take care by compiler based on reference type. Hence method hiding is considered as ‘static polymorphism’ or ‘compile time polymorphism’ or ‘early binding’.

Overriding in the case of Variable

Overriding concept is not applicable for variables. And it is applicable only for methods. Variable resolution always takes care by compiler based on reference type.

class P {
   int i = 888;
}

class C extends P {
   int i = 999;
}

class Test {
   public static void main(String arg[]) {
      P p = new P();
      System.out.println(p.i); //888
      C c = new C();
      System.out.println(c.i); //999
      P p1 = new C();
      System.out.println(p1.i); //888
   }
}

Overriding


Scroll to top