• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Casting problem

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am unable to understand a very simple casting conversion of classes. Please help me clear my concept.!!!
Here is the code.

Employee.java
-------------


Manager.java
------------


ManagerTest.java
----------------


Please help me in understanding this code. This code is shaking my concept of Object conversion type casting.

Thanks in advance.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Siddharth Bhargava:




During Compile time, compiler believes the code and casting that was done. But during runtime , it actually checks the type of the object while casting.

the first line(Manager manager = (Manager)staff[0]; ) works , because staff[0] is actually an object of Manager .

the second assignment will fail , because the staff[1] contains Employee object which cannot be cast to it subclass (Manager)

Always remember the rule , that Manager is a Employee , but all employees cannot be Manager. Try working out is-a relationship and cast it.
So ,
[ December 27, 2008: Message edited by: Juva Yuva ]
 
Sheriff
Posts: 67537
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Juva Yuva", please check your private messages for an important administrative matter.
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But we do cast superclass objects to subclass references. Like for example in the following code.



This example works wherein we are :
1) typecasting superclass reference (o) to the subclass object (A1)
Object o = new A1();
A1 abc = (A1)o;


2) The same thing we are typecasting the superclass reference (a1) to the subclass object (B1) and its methods are being called ab.method() and ab.bmethod()


Its very confusing please help me ....
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*snip*

Sigh. Answer removed due to crosspost: http://forums.sun.com/thread.jspa?threadID=5357652
Please read this: http://faq.javaranch.com/java/BeForthrightWhenCrossPostingToOtherSites
[ December 27, 2008: Message edited by: Bauke Scholtz ]
 
A feeble attempt to tell you about our stuff that makes us money
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic