• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Object Reference Conversion

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Chapter 4 of R&H says, object reference conversion is possible when the conversion is up the hierarcy.
My doubt is, how can a superclass class reference hold a subclass reference???
superclass s = subclass p
Sub class would have additional featues than the superclass right..Wouldn't there be loss of information ???
Thanks in advance.
Regards,
Sushma
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
only the information available to the superclass is available in such assignments.
the specialized info which the subclass contains is not accessible.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sushma,
I think u need to understand the difference between object reference and the object itself. Object reference hold addresses of the actual objects( just like pointers in C++).
The ability of a superclass reference variable to hold a reference to subclass object is enabled so that methods can be written in a generic manner.
For example, take vector class. It has a method add(Object ob)
so that any object can be added to a vector. This is because Object is the superclass of all classes.
No information is actually lost because of this.
Regards,
Venkat.
 
reply
    Bookmark Topic Watch Topic
  • New Topic