1

In the below program I am overriding a static and a instance method, When I call the instance method using the superclass it is executing the subclass method, but in case of the static method it is executing superclass static method. Why is this?

class Superclass
{
  public void print()
  {
    System.out.println("superclass print");
  }
  public static void staticPrint()
  {
    System.out.println("Superclass static print");
  }
}

public class Subclass extends Superclass
{   
  public static void staticPrint()
  {
    System.out.print("subclass staticPrint");
  }     
  public void print()
  {
    System.out.println("subclass print");
  } 
  public static void main(String... args)
  {
    Subclass subclass=new Subclass();
    Superclass superclass=subclass;
    superclass.staticPrint();
    superclass.print();
  }
}

Output:

Superclass static print
subclass print
AlBlue
  • 23,254
  • 14
  • 71
  • 91
Rohit Funde
  • 1,080
  • 1
  • 13
  • 20
  • 3
    Answers below have correctly noted that you can't override static methods. Good practice is to tag your intentional overrides with `@Override`. In your case, that would flag an error, rather than silently hiding the hidden method. – CPerkins Jul 06 '15 at 13:56
  • possible duplicate of [Why doesn't Java allow overriding of static methods?](http://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods) – Erwin Bolwidt Jul 06 '15 at 14:14

3 Answers3

3

Static methods cannot be overridden, the can only be hidden, as for variables. Take a look here for further details:

If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.

The distinction between hiding a static method and overriding an instance method has important implications:

The version of the overridden instance method that gets invoked is the one in the subclass. The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.

Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48
1

There is no method overriding for static methods. superclass.staticPrint() is equivalent to Superclass.staticPrint().

The type of the instance referred by the superclass variable doesn't matter at all when calling a static method. It can even contain null and the static method staticPrint() of Superclass class will still be executed.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

first of all a static method cannot be overridden. when referring to a static method or variable, it will always use the default of the class declared

Subclass subclass=new Subclass();
    Superclass superclass=subclass;
    superclass.staticPrint();

a static method does not need to be instantiated. the only important thing is that superclass was declared as type Superclass.

while an instance method will always refer to its "Instance" implementation as the implementation is dependent on state of an object.

 Subclass subclass=new Subclass();
    Superclass superclass=subclass;
    superclass.staticPrint();
    superclass.print();

since superclass was instantiated as a Subclass. it will call the subclass implementation since an instance method requires a state to execute.

lababo
  • 114
  • 5