Shadow Variables and Inheritance

Discussion in 'Java' started by bfeist@speakeasy.net, Oct 23, 2006.

  1. Guest

    I want to have a static constant X defined in a superclass A and given
    various different values in subclasses B, C, and so on. When the X is
    accessed in a method of A by class B (through inheritance), I want it
    to get the value of B.X, not A.X. Is this possible?

    My initial naive attempt at just defining X separately in each class as
    a static final did not work; A.X is accessed instead. Unless someone
    has a better idea, I'll resort to replacing the constant by a static
    method returning the desired constant instead, and rely on
    polymorphism.

    Any ideas? Thanks in advance.
     
    , Oct 23, 2006
    #1
    1. Advertising

  2. Oliver Wong Guest

    <> wrote in message
    news:...
    >I want to have a static constant X defined in a superclass A and given
    > various different values in subclasses B, C, and so on.


    Right here, there's a conceptual problem. Since X is static, there is
    only "one of" it. There isn't one for A, another for B, another for C, and
    so on. There's just one. And it can take on exactly one value at any point
    in time. And in this case, because it's constant, it has to take the same
    value for the entire program run.

    > When the X is
    > accessed in a method of A by class B (through inheritance), I want it
    > to get the value of B.X, not A.X. Is this possible?


    You would have to declare a different static constant named X in B.

    >
    > My initial naive attempt at just defining X separately in each class as
    > a static final did not work; A.X is accessed instead.


    If you had typed in A.X, then A.X will get access. If you had instead
    typed B.X, B.X would get accessed.

    > Unless someone
    > has a better idea, I'll resort to replacing the constant by a static
    > method returning the desired constant instead, and rely on
    > polymorphism.


    You might have better luck using a non-static method.

    - Oliver
     
    Oliver Wong, Oct 23, 2006
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    >I want to have a static constant X defined in a superclass A and given
    > various different values in subclasses B, C, and so on. When the X is
    > accessed in a method of A by class B (through inheritance), I want it
    > to get the value of B.X, not A.X. Is this possible?
    >
    > My initial naive attempt at just defining X separately in each class as
    > a static final did not work; A.X is accessed instead. Unless someone
    > has a better idea, I'll resort to replacing the constant by a static
    > method returning the desired constant instead, and rely on
    > polymorphism.
    >
    > Any ideas? Thanks in advance.


    There is a way to do this, but it's so hideous that I won't explain it, lest
    someone actually do it.

    If you want polymorphic behavior, use an instance method. If you need the
    value to be a constant (because it's used in, say, a case label), you can
    have your instance method return the value of the constant,e .g.

    class A {
    public static void int THE_ANSWER = 42;

    public int getMagicNumber {
    return THE_ANSWER ;
    }
    }

    class B extends A {
    public static void int THE_QUESTION = 41;

    public int getMagicNumber {
    return THE_QUESTION ;
    }
    }
     
    Mike Schilling, Oct 23, 2006
    #3
  4. Oliver Wong Guest

    "Mike Schilling" <> wrote in message
    news:gj9%g.23143$...
    >
    > class A {
    > public static void int THE_ANSWER = 42;
    >
    > public int getMagicNumber {
    > return THE_ANSWER ;
    > }
    > }
    >
    > class B extends A {


    The question should be:

    public static void int THE_QUESTION = 6 * 9;

    >
    > public int getMagicNumber {
    > return THE_QUESTION ;
    > }
    > }


    And both the question and the answer should be declared final.

    - Oliver
     
    Oliver Wong, Oct 23, 2006
    #4
  5. bfeist Guest

    Oliver Wong wrote:
    > <> wrote in message
    > news:...


    > >I want to have a static constant X defined in a superclass A and given
    > > various different values in subclasses B, C, and so on.

    >
    > Right here, there's a conceptual problem. Since X is static, there is
    > only "one of" it. There isn't one for A, another for B, another for C, and
    > so on.


    > You would have to declare a different static constant named X in B.


    That's what I meant by a shadow variable -- the same name, but declared
    and given values in two different classes.

    > If you had typed in A.X, then A.X will get access. If you had instead
    > typed B.X, B.X would get accessed.


    Alas! That's exactly what happened.

    > > I'll resort to replacing the constant by a static
    > > method returning the desired constant instead

    >
    > You might have better luck using a non-static method.


    What will happen if I use static? Something analagous to my
    experiences with static constants, where there's no dynamic binding
    based on the run-time class involved?

    Thanks,

    Bruce Feist
     
    bfeist, Oct 23, 2006
    #5
  6. bfeist Guest

    Mike Schilling wrote:
    > <> wrote in message
    > news:...
    > >I want to have a static constant X defined in a superclass A and given
    > > various different values in subclasses B, C, and so on. When the X is
    > > accessed in a method of A by class B (through inheritance), I want it
    > > to get the value of B.X, not A.X.


    > There is a way to do this, but it's so hideous that I won't explain it, lest
    > someone actually do it.


    Sir, please don't arouse my curiousity like that unless you plan to
    satisfy it! I don't care how hideous it is, I must know!

    Thanks,
    Bruce
     
    bfeist, Oct 23, 2006
    #6
  7. "Oliver Wong" <> wrote in message
    news:TB9%g.35865$P7.19624@edtnps89...
    >
    > "Mike Schilling" <> wrote in message
    > news:gj9%g.23143$...
    >>
    >> class A {
    >> public static void int THE_ANSWER = 42;
    >>
    >> public int getMagicNumber {
    >> return THE_ANSWER ;
    >> }
    >> }
    >>
    >> class B extends A {

    >
    > The question should be:
    >
    > public static void int THE_QUESTION = 6 * 9;


    Better still:

    public static void int THE_QUESTION = 42; /* 6 * 9 */

    >
    >>
    >> public int getMagicNumber {
    >> return THE_QUESTION ;
    >> }
    >> }

    >
    > And both the question and the answer should be declared final.


    (Regis Philbin voice) Is that your final answer?
     
    Mike Schilling, Oct 23, 2006
    #7
  8. "bfeist" <> wrote in message
    news:...
    > Mike Schilling wrote:
    >> <> wrote in message
    >> news:...
    >> >I want to have a static constant X defined in a superclass A and given
    >> > various different values in subclasses B, C, and so on. When the X is
    >> > accessed in a method of A by class B (through inheritance), I want it
    >> > to get the value of B.X, not A.X.

    >
    >> There is a way to do this, but it's so hideous that I won't explain it,
    >> lest
    >> someone actually do it.

    >
    > Sir, please don't arouse my curiousity like that unless you plan to
    > satisfy it! I don't care how hideous it is, I must know!


    Use reflection.
     
    Mike Schilling, Oct 23, 2006
    #8
  9. Oliver Wong Guest

    "bfeist" <> wrote in message
    news:...
    > Oliver Wong wrote:
    >> <> wrote in message
    >> news:...

    >
    >> > I'll resort to replacing the constant by a static
    >> > method returning the desired constant instead

    >>
    >> You might have better luck using a non-static method.

    >
    > What will happen if I use static? Something analagous to my
    > experiences with static constants, where there's no dynamic binding
    > based on the run-time class involved?


    Right. The rule of thumb is that things which are static don't get
    inherited, and so any rules about inheritance (and thus dynamic binding)
    doesn't apply.

    - Oliver
     
    Oliver Wong, Oct 24, 2006
    #9
  10. Oliver Wong Guest

    "Mike Schilling" <> wrote in message
    news:ara%g.20140$...
    >
    > "bfeist" <> wrote in message
    > news:...
    >> Mike Schilling wrote:
    >>> <> wrote in message
    >>> news:...
    >>> >I want to have a static constant X defined in a superclass A and given
    >>> > various different values in subclasses B, C, and so on. When the X is
    >>> > accessed in a method of A by class B (through inheritance), I want it
    >>> > to get the value of B.X, not A.X.

    >>
    >>> There is a way to do this, but it's so hideous that I won't explain it,
    >>> lest
    >>> someone actually do it.

    >>
    >> Sir, please don't arouse my curiousity like that unless you plan to
    >> satisfy it! I don't care how hideous it is, I must know!

    >
    > Use reflection.


    Beware, though. There is a theory which states that if ever anyone
    discovers how to emulate the inheritance of static members via reflection,
    the Java language as we know it will instantly disappear and be replaced by
    something more bizarrely inexplicable.

    There is another theory which states this has already happened
    (somewhere between 1.4 and 1.5).

    - Oliver
     
    Oliver Wong, Oct 24, 2006
    #10
    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. Barry Kelly
    Replies:
    4
    Views:
    4,791
    Barry Kelly
    Dec 7, 2004
  2. =?Utf-8?B?RGlmZmlkZW50?=

    Question on VS IDE's "build" and "Shadow Copies and Restart"

    =?Utf-8?B?RGlmZmlkZW50?=, Feb 3, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    383
    =?Utf-8?B?RGlmZmlkZW50?=
    Feb 3, 2006
  3. Olivier
    Replies:
    2
    Views:
    3,778
  4. Mark B
    Replies:
    3
    Views:
    2,508
    bruce barker
    Dec 16, 2008
  5. Eva

    shadow copy and deep copy

    Eva, Nov 9, 2010, in forum: Ruby
    Replies:
    2
    Views:
    480
    Robert Klemme
    Nov 9, 2010
Loading...

Share This Page