• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

superclasses- initializing it's fields in the subclass

 
David Duran
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm in super brain fart mode. It must be too many donuts from this morning.
The situation: I've got a superclass that defines a set of like integer values that are required by several subclasses, each subclass defining their own values. However, the subclasses shouldn't have to be instantiated. I'd like to have those values referenced simply as SubberClass.CONSTANT. Is this even possible?

In other words, does anyone have advice on the best way to design this? It seems like I'm asking something that can't be "done" the way I want it:
Declare int constants in a super class, extend and define the values in the subclass and reference those values publically via the class directly, not through a new'd instance. Thanks
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

... publically via the class directly, ...

That just sounds like a static (class) member, unless I'm not understanding you.
Michael Morris
 
David Duran
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, hahaha, so I'm guessing I can't do that. Since if I declare the variables as static in the superclass, then all the subclasses will share the same value, ie they can't set their own individually.
My solution then would be to use public accessor methods which unfortunately can't be static either. I'm trying to get around having to new the subclass because it's not necessary. There's no manipulation of those variables, just retrieval.
 
David Duran
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well hot dang, I just found out about this strange and crazy idea of the "Singleton Pattern". I'm reading up on it on some javaworld articles.
If you know of any other good articles on it, I'd appreciate it. I think the Singleton Pattern is exactly what will help me.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really need the Subclasses? What about using the typesafe enum pattern:

[ March 01, 2003: Message edited by: Ilja Preuss ]
 
Garrett Smith
Ranch Hand
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[Garrett-Smiths-Computer:java/practice/static] garrett% java SubberClass
10
10
0
The thing to remember is that you're hiding the variable and that hiding is limited to the subclass. If you want SubberClass value of the constant, you'll have to use CONSTANT within SubberClass or use SubberClass.CONSTANT.
Here's a program that demonstrates the scope hidden static members.


In addition to hiding static variables, you can hide static methods, and even instance methods and variables. See Ch 8 of the JLS.
It's a stupid trick and totally impractical. I wouldn't do this in a professional setting, or even in my own development, especially if you're prone to donut-induced confusion!
(fixed the forum tags)
[ March 01, 2003: Message edited by: Garrett Smith ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic