• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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: 22510
122
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: 74381
334
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic