polymorphic behaviour from class constant

Discussion in 'Java' started by Thomas Britton, May 1, 2004.

  1. 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
    Thomas Britton, May 1, 2004
    #1
    1. Advertising

  2. Thomas Britton

    Chris Uppal Guest

    Thomas Britton wrote:

    > 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
    Chris Uppal, May 2, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Elliott

    Q: How to copy a polymorphic class?

    Simon Elliott, Oct 28, 2003, in forum: C++
    Replies:
    12
    Views:
    614
    Rolf Magnus
    Oct 29, 2003
  2. S. Levent Yilmaz
    Replies:
    5
    Views:
    3,730
    Peter Koch Larsen
    Nov 26, 2003
  3. Laurent Deniau

    Implementing polymorphic behaviour

    Laurent Deniau, Oct 24, 2007, in forum: C Programming
    Replies:
    3
    Views:
    420
    Daniel Kraft
    Oct 24, 2007
  4. Alan Woodland
    Replies:
    12
    Views:
    538
    Marco Manfredini
    Nov 22, 2007
  5. Martin Thiede
    Replies:
    0
    Views:
    102
    Martin Thiede
    Feb 14, 2009
Loading...

Share This Page