0

Why is it impossible to have a static method in the subclass with the same signature as in parent class?

class Parent {
    public final static void fun() {
        System.out.println("parent");
    }
}

public class Child extends Parent {        
    // doesn't compile! 
    public static void fun() {
        System.out.println("child");
    }
}

I just want to find out why they allow final modifier here? We all know that static methods belong to a class and not to object, so it is impossible to override the method in the subclass. So as for me final in redundant here.

Finkelson
  • 2,921
  • 4
  • 31
  • 49
  • you can redefine static method in subclass. +1 to @Michael's answer to pointing exact reason of compilation failure. – Braj Sep 19 '15 at 19:27

1 Answers1

2

You declared that method as final, which means you sealed it. A final method cannot be redefined by a subclass. Per Oracle,

You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses

Michael
  • 2,673
  • 1
  • 16
  • 27