This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract and static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract and static" Watch "abstract and static" New topic
Author

abstract and static

Basanti Mathad
Ranch Hand

Joined: Aug 27, 2002
Posts: 60
can anyone tell me why can a method not be both abstract and static.
thank you
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Because static methods cannot be overridden, that is, they do not exhibit polymorphic behaviour.
An abstract method is meant to be provided an implementation by one overridding method in a subtype of the type where it was declared. The abstract method was born to be overriden.
However a static method already has an implementation that is independent of any instance of its class, and of course, independent of instances of subtypes. The implementation of a static method is bound to the type where it was declared. This is the opposite to polymorphism. The static method was born to be called as it is, without the need to create any instance.


SCJP2. Please Indent your code using UBB Code
Basanti Mathad
Ranch Hand

Joined: Aug 27, 2002
Posts: 60
I got it.
Thank u so much....
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

To understand this -- you really need to have a good understanding of some basic OO ideas -- like inheritance and overriding.
When you extend a class, you inherit all of its accessible instance members (accessibility is of course defined by the access modifiers: private, public, protected or the default access level -- no modifier). Notice I said you inherit its instance members -- not the static ones that belong to the class, and not the object.
On the other side you have the modifier abstract -- by definition, if you declare a method as abstract you're saying that this method has to be overridden by the subclass unless of course you declare the subclass as abstract too... and then you've gotta do the whole thing over again with it's subclass -- but that's beside the point.
So -- what we've got so far:
-- a static method will not be inherited when by your subclass.
-- an abstract method MUST be overriden by the subclass.
See how the two conflict? You can't have something defined as static abstract because then you're saying "This thing belongs to the class, not the object, so it won't be inherited when this class is extended; on the other hand -- this method MUST be overridden when this class is extended" -- a total contradiction -- so the compiler won't allow it.
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
Dear Basanti,
Check the answer here in this link
http://www.artima.com/legacy/design/interfaces/messages/39.html
Good Luck
Sridhar
Basanti Mathad
Ranch Hand

Joined: Aug 27, 2002
Posts: 60
Thank u all very much...
I could get the reason why its impossible.
thanks again...
basanti
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I would say that static members are inherited. Though static methods cannot be overridden, but hidden.
From JLS 8.4.6

A class inherits from its direct superclass and direct superinterfaces all the non-private methods (whether abstract or not) of the superclass and superinterfaces that are accessible to code in the class and are neither overridden (�8.4.6.1) nor hidden (�8.4.6.2) by a declaration in the class.

Hidden only applies to static members.
 
jQuery in Action, 2nd edition
 
subject: abstract and static