Why subclass variable can't reference a superclass object?

Discussion in 'Java' started by Matt, Apr 25, 2004.

  1. Matt

    Matt Guest

    class A
    { //etc...
    }

    class SubClassA extends A
    { //etc...
    }

    I know subclass variable can't reference a superclass object,
    but only superclass variable can reference a subclass object.
    But I don't understand the rationale.

    Please advise. Thanks!
    Matt, Apr 25, 2004
    #1
    1. Advertising

  2. Matt

    Chris Smith Guest

    Matt wrote:
    > class A
    > { //etc...
    > }
    >
    > class SubClassA extends A
    > { //etc...
    > }
    >
    > I know subclass variable can't reference a superclass object,
    > but only superclass variable can reference a subclass object.
    > But I don't understand the rationale.


    I'm I'm understanding your question, you've got it backwards. The
    subclass (SubClassA) can reference members of the superclass (A), as
    long as the access specifiers are chosen to make this possible.
    However, the superclass can't access members of the subclass.

    As for the rationale, it's because a superclass can have multiple
    subclasses, as in:

    class A { ... }
    class SubClassA extends A { ... }
    class SubClass2A extends A { ... }

    Now, SubClassA may declare one set of members, but SubClass2A a
    completely different set. When you're writing code in class A, you
    don't know whether the object is really a plain A, or a SubClassA, or a
    SubClass2A. So which set of subclass members would you need to access?

    (If that's not your question, then please clarify.)

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Apr 25, 2004
    #2
    1. Advertising

  3. Matt

    Bjorn Abelli Guest

    "Matt" wrote...
    >
    > class A
    > { //etc...
    > }
    >
    > class SubClassA extends A
    > { //etc...
    > }
    >
    > I know subclass variable can't reference a superclass object,
    > but only superclass variable can reference a subclass object.
    > But I don't understand the rationale.


    Think of it as a hierarchical taxonomy:

    An animal (superclass) can be of many types, such as a dog, a platypus, etc
    (subclasses).

    Hence, we can say that a dog "is an" animal, as well as a platypus "is an"
    animal.

    But we can't say that an animal "is a" dog, as it isn't true in *every*
    case. An animal doesn't need to be a dog.

    http://mindprod.com/jgloss/subclass.html

    When a subclass extends a superclass, it can also have additional attributes
    and methods not existing in the superclass.

    As the variable type determines what methods you can use "on" it, a variable
    of a subclass type can hence expose more operations available than the
    superclass has implemented.

    That's why a variable of a subclass type can't reference an instance of a
    superclass.

    Example:

    class Animal {}
    class Dog extends Animal {}

    ....

    Animal a = new Dog(); // allowed
    Dog d = new Animal(); // not allowed

    ....

    P.S. This kind of questions should rather be asked in
    comp.lang.java.help.

    // Bjorn A
    Bjorn Abelli, Apr 25, 2004
    #3
  4. Matt

    Tony Morris Guest

    "Matt" <> wrote in message
    news:...
    > class A
    > { //etc...
    > }
    >
    > class SubClassA extends A
    > { //etc...
    > }
    >
    > I know subclass variable can't reference a superclass object,
    > but only superclass variable can reference a subclass object.
    > But I don't understand the rationale.
    >
    > Please advise. Thanks!


    Assuming you have it "the other way around":

    http://www.xdweb.net/~dibblego/java/faq/answers.html#q32

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
    Tony Morris, Apr 26, 2004
    #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,459
    Woebegone
    Dec 18, 2003
  2. jstorta
    Replies:
    3
    Views:
    428
    jstorta
    Feb 20, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,757
    Smokey Grindel
    Dec 2, 2006
  4. Evan Klitzke
    Replies:
    0
    Views:
    346
    Evan Klitzke
    Aug 2, 2007
  5. bart van deenen
    Replies:
    6
    Views:
    734
    bart van deenen
    Mar 3, 2009
Loading...

Share This Page