• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Override Inherited Constants?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to develop a class that has a certain structure, much like an interface, but I would like it to be abstract. I plan to extend this class ALWAYS so that it could be abstract. Also, I want the base class to contain constants that would HAVE TO BE overridden by any inheriting class. Can this be done?

Thanks!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like these "constants" aren't very constant. But in any case, you can't override variables.

Can you tell us more about what you're trying to do? If the base is always going to be extended, and the variables have to be "overridden," then what is the purpose of valuing them in the base?
 
Jason Barker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I'm trying to do is create a class that will always be subclassed. I would like for the class to always have values (preferrably constants over data members) that would change with each type of subclass. For example, the parent class would have certain _TYPE_ variables that would have unique values for each subclass but should be available as static, unalterable variables.

Any ideas?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a variable needs to be "unalterable," then you want it to be final. But if it's static and final, then the value has to be set at the point of declaration (or within some static block). In other words, you can't value a static final variable upon instantiation, because by that time it already has a value that can't be changed.

In the code below, a variable is declared as final (but not static) in the abstract base class. Because it's final, it must be valued by the time the constructor exits, so it's valued in the constructor. Instances of the extending class pass a value to the base constructor with a call to super(int i), and that locks in a final value for that instance.

Is that what you're looking for?
 
Jason Barker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I'm trying to do:


errors.jsp file<br/>


Basically, I could subclass the WebForm as much as I want and it would require that the subclasses would implement certain constants and methods. So how can I accomplish this?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, you can't override variables. If you re-declare them in an extending class, then you're just "hiding" them...

So how do you require subclasses to value "constants" declared in the superclass? I think the code I posted earlier does that.

By the way, only classes, interfaces, and methods can be abstract. A variable can't be abstract. If a variable is static and final, then it must be assigned a value at declaration or within static block. (If it's final without being static, then it must be assigned a value by the time the constructor exits.) So you're not going to have much luck with...

abstract static final String NAME;
 
Jason Barker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, that's what I thought. I was just wondering if anyone could enlighten me on a different, legal way to accomplish what I am trying to do.


Thanks!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic