wood burning stoves*
The moose likes Beginning Java and the fly likes Inheritance Variable assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance Variable assignment" Watch "Inheritance Variable assignment" New topic
Author

Inheritance Variable assignment

Tim-Ting Chang
Greenhorn

Joined: Jan 13, 2009
Posts: 25
What happens when we assign a new subclass instance to a superclass variable identifier?

Specifically, what happens if I call subclass-only fields and methods from a superclass variable identifier?

e.g.

What's the output of Test?

I want to know the rationale behind the output, that's why I asked here rather than compiling it.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

why don't you try and figure out the output yourself, and then run it and see if you're. right. If so, AWESOME!!!.

If you're wrong, try and figure out WHY you're wrong.

If you can't figure out why you're wrong, come back and ask us why the output is different from what you expect (and please tell us what you expected, what you got, and WHY you thought what you did).

THAT'S going to be the best way for you to learn.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
(I don't want to circumvent anyone's learning experience, but...) Tim-Ting, your subclass declarations need to use the "extends" keyword. The way you've coded it, those classes are not subclassing SuperClass. Once you've done that, you can start exploring the question you raised.
 
jQuery in Action, 2nd edition
 
subject: Inheritance Variable assignment