polymorphic behaviour from class constant

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
 
C

Chris Uppal

Thomas said:
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...?

It may be clumsy, but that's the only way to do it.

I don't see why you should object to using a class variable in an instance
method unless you are thinking of the get_max_age() method as being an
"accessor" for the class variable. If that is the case then the solution is
simple: stop thinking of it as an accessor ;-) I'd be inclined to change the
name anyway -- it isn't "getting" anything, it's just a constant-valued method,
so a name like maxLifeSpan() seems more appropriate to me.

BTW, the method in the subclass isn't really a duplicate of the method in the
superclass, as you could make clear by (for instance) explicitly qualifying the
name of the static field.

class Human
{
public static final int MAX_AGE = 100;
public int get_max_age() { return Human.MAX_AGE; }
}

class Uebermensch extends Human
{
public static final int MAX_AGE = 2000;
public int get_max_age() { return Uebermensch.MAX_AGE; }
}

Or by using different names for the two constants (it's questionable whether
the Uebermensch class should be hiding the value of Human.MAX_AGE anyway -- it
seems reasonable that overmen should "know" how long us normal types live too.)

BTW, if this were a real example, then I'd question the need for the MAX_AGE
fields at all. Unless they are intended to be used independently of the
get_max_age() method (which they may well be) then the common proscription
against embedding "magic numbers" in code does not apply. The point is that if
those conditions are met, then the get_max_age() method is itself the single
"point of contact" with the actual value (100 or 2000), and /that method/ then
is the self-documenting replacement for the magic number. The actual value
only appears once in the code, and all other mentions of it are "by name",
which is all the proscription is trying to achieve.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top