Hello everyone!
This may be a bit of a dumb question, but I think this is the only way to
get where I want to be.
Lets say I have an abstract BottledWater class and two classes, Evion and
Aquafina extending from it.
public abstract class BottledWater {
abstract public static final String NAME; // Compile-time error.
}
public class Aquafina extends BottledWater {
public static final String NAME = "Aquafina";
}
public class Evion extends BottledWater {
public static final String NAME = "Evion";
}
Currently, the abstract constant declaration in BottledWater gives a
"Modifyer abstract not allowed here" compile-time error. Is there any way to
require Evion and Aquafina declare their own NAME variable? I thought a
public static constant would be best, since in the scenerio I'm working
with, the names would always be the same, and all classes of the same type
would have the same name.
Thanks in advance!
Roedy Green - 06 May 2004 02:25 GMT
> abstract public static final String NAME; // Compile-time error.
take out the abstract. There is no way in Java to force someone to
shadow that.
You could do it this way:
abstract public String getName ()
then they have to provide an implementation.
This is what you want anyway. Otherwise the only way to get the
string is via the specific class name.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Inertia_sublimation - 07 May 2004 06:13 GMT
> abstract public String getName ()
> then they have to provide an implementation.
There is no way to require abstract static methods either? :-(
Roedy Green - 07 May 2004 06:20 GMT
>There is no way to require abstract static methods either? :-(
Nope. Interfaces and abstract force you to create instance methods
only.
Since you can't use static stuff in a generic way, there is not much
point in putting it in an interface, though I too have often wished I
could do something like "You should write a static method like this"
to have a complete useful class.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.