• 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

Object Reference and Class object

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

I was refering to a Java certification book ( A PROGRAMMER'S GUIDE TO JAVE CERTIFICATION , BY KHALID A. MUGHAL AND ROLF W.RASMUSSEN) and the Topic is Object Oriented Programming ( Chapter 6 ) where it has specified " A CAST ONLY CHANGES THE TYPE OF REFERENCE NOT THE CLASS OF THE OBJECT ".

Can some one help me in understanding this sentence.

Thanks in Advance.
Murali
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Muralidhar Mandapati:
" A CAST ONLY CHANGES THE TYPE OF REFERENCE NOT THE CLASS OF THE OBJECT ".

This is the distinction between the type of a reference and the type of an object or instance. When you instantiate an object and assign a reference to it to a variablethe object (instance) that gets created has a permanent type: its class, ArrayList in the example. But since the object reference l is declared as List, you cannot use it to call methods declared only in ArrayList.

If you want to call those methods, you have to cast the reference. Casting tells the compiler that you expect the reference to point to an object with the given type or one of its subtypes, but it does not alter the object's type.
 
Muralidhar Mandapati
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I think i understood.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic