Why subclass variable can't reference a superclass object?

M

Matt

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!
 
C

Chris Smith

Matt said:
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
 
B

Bjorn Abelli

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
 
T

Tony Morris

Matt said:
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
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top