• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

question about declare and create an object

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 2 classes and one is subclass of another like below:



The first one is correct one but the second. I do not have a good explanation for that. I do know that

A reference variable can refer to any object of its declared type or any subtype of its declared type


and why not

A reference variable can refer to any object of its declared type or any supertype of its declared type


Im not convinced myself. Please help, thanks
 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"A super class reference variable can refer to any direct or indirect sub class object, but it reverse is not allowed."

And the reverse is not allowed becuse it is possible that super class might have members (privete) which are not available in sub class.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try replacing A and B with more concrete examples:
 
Marshal
Posts: 79968
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:"A super class reference variable can refer to any direct or indirect sub class object, but it reverse is not allowed."

That is correct, but please tell us where you found it, to avoid copyright problems, etc.

And the reverse is not allowed becuse it is possible that super class might have members (privete) which are not available in sub class.

That doesn't seem a very good explanation; looking at Rob's post with Dog and Animal gives an easier explanation to understand.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic