Wednesday, April 18, 2007

Why an abstract method cannot be static?

An abstract method is usually defined in an abstract class or an interface,for which implementation is provided in a subclass or a class implementing the interface.As static methods just have single copy per class and are interpreted at code compile time,not at runtime, so it is impossible to have polymorphic behaviour out of them.In other words, they cannot be overridden.

An abstract class is one which cannot be instantiated but a static method defined in abstract class can be invoked without creating an instance.So there is no mechanism to ensure call of an abstract static method.

Moreover this is a design decision by language designers. :-)

3 comments :

Abhijeet Rathod on July 2, 2008 at 6:31 AM said...

More Explaination:
* static methods are final by default.
* final methods can't be overriden.
* abstract methods needs to be overidden in Child class whichever extending Abstract class.

http://abhijeetrathod.blogspot.com

Anonymous said...

An abstract class may have static fields and static methods. You can use these static members with a class reference—for example, AbstractClass.staticMethod()—as you would with any other class.
taken from
http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html

Anonymous said...

Static methods are not final by default !

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=1224787

 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2017. Reads: best tracker