Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Reference casting question

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

I got one question related to reference casting. Here is code snippet:



Why is it okay when we cast d = (Derived)b but we got error java.lang.ClassCastException in case of Derived d2 = (Derived) new Base()

Best regards,
Denis
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are mistaken,

The error will be thrown at first cast, it will never get to the second.
 
Denis Berezhnoy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jesse,

I am sorry but I did not get what you mean. I checked again casting d = (Derived)b; does not generate exception. This is the same when we get object from collection and cast it to needed type.

Here is full code:



Best regards,
Denis
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error occurs on line
d=(Derived)b;
since d can never ever point towards the base class reference so the question of casting can never arise...
Think about it...
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

everyone should have a look at the complete code. In the first posting, a very important line is missing.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It is ok, if you try to assign Base class object to the derived class
reference variable with casting. But it is ok for compile time. It will fail
at run time. JVM recognizes that this is wrong you have done and compiler
relied on you because it saw the cast.

Derived class ref variable can't hold the object of base class.


Thanks,
 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi two all..
Yeah i figured out my mistake...but have also found out the solution
the answer to the problem is very well defined in K&B book you should look at page number 113 chapter 2 under reference variable casting and you will get the answer...
In brief the answer can be stated as
The object of the class Base does not know anything about the derived class untill the reference of it points to the the object of class derived since it has all the things a base contained hence the statement...

Derived d2= (Derived)new Base(); will compile but will produce an exception.
Another senario like the above which will compile but will produce an exception is
Base b= new Base();
Derived d= (Derived)b;//compile but will produce exception

This is a very good point about reference casting and must be carefully noted...
good question indeed.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hope you got it.
 
It's just a flesh wound! Or a tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic