Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

[object can't cast]????

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcus Green mock exam..
class Base {}
class Sub extends Base {}
public class A{
public static void main(String argv[]){
Base b=new Base();
Sub s=(Sub) b;
}
}
compile without error,but with runtime exception...why??
only primitive data can cast??
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Liao,
Try this instead!
code:
class Base {}
class Sub extends Base {}
class A{
public static void main(String argv[]){
Base b=new Base();
//Sub s=(Sub) b;//Line :1 Here is the runtime exception!
Base c= new Sub();
Sub d = (Sub) c;
}
}
What happens is that in compile time the compiler only checks the references have compatibilty or not, as you have casted it properly in line : 1. But in runtime a subclass cannot caontain a superclass object. Here i m a little whether i m right about in runtime what happens.
Someone expalin plz....

------------------
azaman
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code would compile and run if it were changed to
Base b = new Sub(); //As casting to parents are always allowed
Sub s = (Sub) b; //As b is really an instance of the Sub class, there is no casting problem.
In your case the compiler does not omplain as you have used the cast. The complier checks for type of b and is assured that it can contain objects of type Sub. So it passes compilation. However at runtime , the object in b is not of type Sub but of type base - its parent. This sort of casting is not allowed. So the JRE complains.
I hope it is clear, if not please let us know.
 
Ashik Uzzaman
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sunetra, U have cleared all my confusions....
------------------
azaman
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic