subclass has a variable that is subclass of same superclass

Discussion in 'Java' started by jstorta, Feb 19, 2006.

  1. jstorta

    jstorta Guest

    I have been running this scenario in my head for a while. In the
    surface it seems fine, but I keep thinking that somehow it will cause
    problems.


    class A {
    }

    class B extends A {
    }

    class C extends A {
    B myB;
    }

    Class B is a A

    Class C is also a A but also has a B which is in turn a A.


    Is there anything wrong with having a variable in a class of a type
    that inherits from the same parent that the class itself inherits from?


    Thanks.
     
    jstorta, Feb 19, 2006
    #1
    1. Advertising

  2. "jstorta" <> wrote in message
    news:...
    >I have been running this scenario in my head for a while. In the
    > surface it seems fine, but I keep thinking that somehow it will cause
    > problems.
    >
    >
    > class A {
    > }
    >
    > class B extends A {
    > }
    >
    > class C extends A {
    > B myB;
    > }
    >
    > Class B is a A
    >
    > Class C is also a A but also has a B which is in turn a A.
    >
    >
    > Is there anything wrong with having a variable in a class of a type
    > that inherits from the same parent that the class itself inherits from?


    Not at all. Consider that A might be "java.lang.Object".
     
    Mike Schilling, Feb 19, 2006
    #2
    1. Advertising

  3. Mike Schilling wrote:
    > "jstorta" <> wrote in message
    > news:...
    >
    >>I have been running this scenario in my head for a while. In the
    >>surface it seems fine, but I keep thinking that somehow it will cause
    >>problems.
    >>
    >>
    >>class A {
    >>}
    >>
    >>class B extends A {
    >>}
    >>
    >>class C extends A {
    >> B myB;
    >>}
    >>
    >>Class B is a A
    >>
    >>Class C is also a A but also has a B which is in turn a A.
    >>
    >>
    >>Is there anything wrong with having a variable in a class of a type
    >>that inherits from the same parent that the class itself inherits from?

    >
    >
    > Not at all. Consider that A might be "java.lang.Object".


    Nicely put!
     
    Jeffrey Schwab, Feb 20, 2006
    #3
  4. jstorta

    jstorta Guest

    It makes perfect sense when you put it that way.

    Thanks.
     
    jstorta, Feb 20, 2006
    #4
    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. Pat Ryan
    Replies:
    28
    Views:
    6,615
    Woebegone
    Dec 18, 2003
  2. Matt
    Replies:
    3
    Views:
    15,732
    Tony Morris
    Apr 26, 2004
  3. Tobias K.
    Replies:
    16
    Views:
    5,449
    Mike Schilling
    Nov 22, 2004
  4. Evan Klitzke
    Replies:
    0
    Views:
    402
    Evan Klitzke
    Aug 2, 2007
  5. bart van deenen
    Replies:
    6
    Views:
    827
    bart van deenen
    Mar 3, 2009
Loading...

Share This Page