T
Thomas Britton
Hi,
I wonder if anyone can tell me the best way (or a good way) of
accessing a class constant from an object, polymorpically?
(I hope my terminology is correct... I'm a bit rusty!)
Here is an example that illustrates what I am trying to achieve,
but it seems a bit clumsy to (a) duplicate a method in a derived
class and (b) access a class variable using an instance method...?
class Human
{
public static final int MAX_AGE = 100;
public int get_max_age() { return MAX_AGE; }
}
class Uebermensch extends Human
{
public static final int MAX_AGE = 2000;
public int get_max_age() { return MAX_AGE; }
public static void main(String[] args)
{
Human[] men = new Human[2];
men[0] = new Uebermensch();
men[1] = new Human();
for(int i=0; i < men.length; i++) {
System.out.println("Man No. " + i +
" will never be older than " +
men.get_max_age());
}
}
}
Any advice gratefully received!
/tom
I wonder if anyone can tell me the best way (or a good way) of
accessing a class constant from an object, polymorpically?
(I hope my terminology is correct... I'm a bit rusty!)
Here is an example that illustrates what I am trying to achieve,
but it seems a bit clumsy to (a) duplicate a method in a derived
class and (b) access a class variable using an instance method...?
class Human
{
public static final int MAX_AGE = 100;
public int get_max_age() { return MAX_AGE; }
}
class Uebermensch extends Human
{
public static final int MAX_AGE = 2000;
public int get_max_age() { return MAX_AGE; }
public static void main(String[] args)
{
Human[] men = new Human[2];
men[0] = new Uebermensch();
men[1] = new Human();
for(int i=0; i < men.length; i++) {
System.out.println("Man No. " + i +
" will never be older than " +
men.get_max_age());
}
}
}
Any advice gratefully received!
/tom