• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance Variable assignment

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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.
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic