Why static methods of parent class gets hidden in child class in java?

Java 8Object Oriented ProgrammingProgramming

When we have two classes where, one extends another and if, these two classes have same method including parameters and return type (say, sample) the method in the sub class overrides the method in the super class.

i.e. Since it is inheritance. If we instantiate the subclass a copy of superclass’s members is created in the subclass object and, thus both methods are available to the object of the subclass.

But if you call the method (sample), the sample method of the subclass will be executed overriding the super class’s method.

Example

class Super{
   public static void sample(){
      System.out.println("Method of the superclass");
   }
}
public class OverridingExample extends Super {
   public static void sample(){
      System.out.println("Method of the subclass");
   }
   public static void main(String args[]){
      Super obj1 = (Super) new OverridingExample();
      OverridingExample obj2 = new OverridingExample();
      obj1.sample();
      obj2.sample();
   }
}

Output

Method of the superclass
Method of the subclass

Overriding Static Methods

When superclass and subclass contains same method including parameters and if they are static. The method in the superclass will be hidden by the one that is in the subclass.

This mechanism is known as method hiding in short, though super and subclasses have methods with same signature it they are static, it is not considered as overriding.

Example

class Super{
   public static void demo() {
      System.out.println("This is the main method of the superclass");
   }
}
class Sub extends Super{
   public static void demo() {
      System.out.println("This is the main method of the subclass");
   }
}
public class MethodHiding{
   public static void main(String args[]) {
      MethodHiding obj = new MethodHiding();
      Sub.demo();
   }
}

Output

This is the main method of the subclass

Reason for method hiding

The key in method overloading is, if the super class and sub class have method with same signature, to the object of the sub class both of them are available. Based on the object type (reference) you used to hold the object, the respective method will be executed.

SuperClass obj1 = (Super) new SubClass();
obj1.demo() // invokes the demo method of the super class
SubClass obj2 = new SubClass ();
obj2.demo() //invokes the demo method of the sub class

But, in case of static methods, since they don’t belong to any instance, you need to access them using the class name.

SuperClass.demo();
SubClass.Demo();

Therefore, if a super class and sub class have static methods with same signature, though a copy of the super class method is available to the sub class object. Since they are static the method calls resolve at the compile time itself, overriding is not possible with static methods.

But, since a copy of static method is available, if you call the sub class method the method of the super class will be redefined/hidden.

raja
Published on 05-Aug-2019 17:33:16
Advertisements