2

I know in Java, static method can not be overriden by the subclass.

Two questions:

1. Why is that? Could anyone explain me the reason inside it?

2. Can subclass override final method in super class then?

2
  • Why don't you try number 2 yourself? Commented Dec 17, 2011 at 19:34
  • Static methods in Java can not be resolved dynamically at run time hence, they can not have polymorphic behavior and there is no question about overriding static methods. Commented Dec 17, 2011 at 19:41

1 Answer 1

12

Static methods aren't called on a particular instance - so they can't be called polymorphically. They are called on the type itself - nothing about the binding relies on any information which is only available at execution time. The point about polymorphic calls is that the method implementation which ends up being executed depends on the execution-time type of the target of the call; there's no target for static method calls, as such.

No, subclasses can't override final methods - the whole point of making a method final is to prevent it from being overridden.

Sign up to request clarification or add additional context in comments.

2 Comments

@Leem.fin: You could potentially regard the class as the target, but it's certainly not something which could vary at execution time. You certainly can declare another static method in the same signature in a subclass, but that doesn't override - it hides or shadows it. That's very different, and it's really important to understand why.
@Leem.fin You should never call static functions through an instance. Hence the only way to call a static class is something like Foo.bar() where you explicitly specify the class that should be used, so dynamic dispatch doesn't make any sense. Nothing stops you from hiding the static method in your subclass though (apart from good taste - hiding static methods is less problematic than all other cases, but I still would try to avoid it)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.