• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

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: 22743
129
Eclipse IDE Spring VI Editor 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: 77926
373
  • 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.
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic