-1

I was able to run the following code:

class A
{
    public static void display()
    {
        System.out.println("Inside static method of superclass");
    }
}

class B extends A
{
    public void show()
    {
        display();
    }
}

public class staticMethodInheritance {
    public static void main(String[] args) {
        B b = new B();
        b.display();
    }
}

Now I was able to access the method display() from the instance of the class B then why is it said that static methods cannot be inherited. If I declare a method display in class B then it is said that the method in the superclass is hidden and the method in the child class is called then again isnt this the behavior that is desired when we override a method.

takendarkk
  • 3,347
  • 8
  • 25
  • 37
Nick Div
  • 5,338
  • 12
  • 65
  • 127

1 Answers1

0

The only difference with inherited static (class) methods and inherited non-static (instance) methods is that when you write a new static method with the same signature, the old static method is just hidden, not overridden.

Static methods are inherited but cannot be overriden they can be re- defined.

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • What do you mean exactly by hidden and not overridden. I dont see the difference in the behavior – Nick Div Nov 21 '14 at 05:44
  • @kapilchhattani more read [here](http://stackoverflow.com/questions/10291949/are-static-methods-inherited-in-java) and [here](http://www.geeksforgeeks.org/can-we-overload-or-override-static-methods-in-java/) – Ankur Singhal Nov 21 '14 at 05:46
  • I know these concepts but I guess I havent framed my question well. But anyways thanks for the answer, I'll mark yours as the correct one. – Nick Div Nov 21 '14 at 05:53