• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

static and abstract

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a small code snippet, which declared a abstract static method. The code gave me a compile error. Can any one tell me why can't I have a static method declared as abstract ?
-Sam
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because static methods are resolved at compile time and do not participate in polymorphism. Therefore they can never be over-ridden (they can be hidden with another static method with the same name, but they can't be over-ridden). Therefore it would never be implemented. Therefore a compile error.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sam pitt:
I wrote a small code snippet, which declared a abstract static method. The code gave me a compile error. Can any one tell me why can't I have a static method declared as abstract ?
-Sam



Hi Sam,
First, 'static' method is a class method (i.e. cannot be overriden)
Second, a class with an abstract method cannot be initialized. Such class is used as a base for the subsequent subclass(es).
Third, any subsequent subclass of an abstract class, if it must be enabled for object creation or initialization, must provide a body for all abstract methods of its superclass as well as all the parent classes in the ancestory tree. That means such subclass must override the superclass's abstract method.
Overriding superclass's method is not possible if the superclass's method is declared with 'static' modifer as stated earlier.
Hope that helps,
Lam
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam,
When you declare a method as abstract, you agree to imlement that method in the subclasses by overriding it. On the contrary static methods cannot be overridden (they can only be hidden in the sub classes). Thus static and abstract combination leads to a potentially contradictory situation and hence is tagged as error.
I have made some useful notes on such conditions. You may find it useful http://www.tipsmart.com/studytools/revtips.htm
Sandeep Nachane
------------------
www.tipsmart.com
[This message has been edited by Sandeep Nachane (edited April 27, 2001).]
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic