Overriding

Overriding

In this tutorial, we are going to discuss overriding in Java. Java has been a very well-known general-purpose language. That is object-oriented, class-based in nature, along with primitive data types, which are also known to contain few implementation-related dependencies, which make the working of the application developers very easy as it provides them with ease of writing once and read anywhere.

Method Overriding in Java

Whatever the parent has by default available to the child class through inheritance, If the child class is not satisfied with the parent class implementation, the child is allowed to overwrite that parent class method to provide its 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 marry() {
      System.out.println("Keerthi");
   }
}
/**
 * 
 * @author ashok.mariyala
 *
 */
public class C extends P {
   public void marry(){
      System.out.println("Priyanka");
   }
}
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public static void main(String[] args) {
      P p1 = new P();
      p1.marry(); //Keerthi
      C c1 = new C();
      c1.marry(); //Priyanka
      P p2 = new C();
      p2.marry(); //Priyanka
   }
}

Overriding method resolution will take care of by JVM based on the run time Object. Hence overriding is considered as run time polymorphism or dynamic polymorphism, or late binding.

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

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.
  • The 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 the same declaration in the 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 to 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, there is no compile-time or run time error. It seems that overriding happens, 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 are declared as static.

In the case of method hiding, method resolution will take care of by the 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 of by the compiler based on the reference type. Hence method hiding is considered ‘static polymorphism’ or ‘compile time polymorphism’ or ‘early binding.’

Overriding in the case of Variable

The overriding concept is not applicable for variables. And it is applicable only for methods. Variable resolution is always taken care of by the compiler based on the 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