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