0

I read somewhere that overriding an static methods can not be considered polymorphism. Just hiding the method. What does it mean "hide"?


public class TesterClass {
    public static void main(String[] args) {
        ClassLetters.staticM();
        ClassA.staticM();

        ClassLetters Lettersobj = new ClassA();
        ClassA Aobj = new ClassA();


        Lettersobj.staticM();
        Aobj.staticM();
        ClassA aa = (ClassA) Lettersobj;
        aa.staticM();
    }
}

Output:

Static Method Called in CLassLetters
Static Method Called in ClassA
Static Method Called in CLassLetters
Static Method Called in ClassA
Static Method Called in ClassA

classA inherits ClassLetters with same static void classM. Why it is not polymorphism?

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Naisyak
  • 83
  • 1
  • 2

1 Answers1

3

You can't override static members, because they are not inherited. They belong to the class itself, period.

If you create a subclass, and you create a (new) static method with the same name/parameters/... you don't override the original one, you re-define a new one.

So, the static method in your original class is not overridden, but hidden.

Stultuske
  • 9,296
  • 1
  • 25
  • 37