2

How can I override instance methods of super class as static in subclass? I think this is impossible but is there any indirect way?

public class A {
   public void test(){
       System.out.println("");
   }
}

public class B extends A{
    public static void test(){//test() in B cannot override test() in A 
    //overriding method is static

   }
}
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
Sarkhan
  • 1,281
  • 1
  • 11
  • 33

1 Answers1

8

You can't, since a static method is not an instance method. You could override the instance method with an instance method that calls the static method.

public class B extends A {
    @Override
    public void test(){
        staticTest();
    }
    public static void staticTest() {
    }
}

I'm not sure how much sense that would make though.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    It won't make any difference! :D – Pratik Apr 30 '15 at 08:26
  • @Pratik What do you mean? If you call `test()` on an instance of B, it will execute `B`'s version of `test()`, which would call the static method. – Eran Apr 30 '15 at 08:27