• 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

class cast exception

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class fruit
{
public void popo1()
{
System.out.println("this is a popo1 of the fruit class");
}
}
interface squeezable
{
}
class citrus extends fruit implements squeezable
{
public void nitin()
{
System.out.println("what the hell is going on");
}
}
class lemon extends citrus
{
}
class grapefruit extends citrus
{
public void nitin()
{
System.out.println("what r u trying to do");
}
}
class lapore
{
public static void main(String[]args)
{
grapefruit g[]=new grapefruit[50];
for(int i=0;i<g.length;i++)>
{
g[i]=new grapefruit();
}
citrus[] s=g;
for(int i=0;i<s.length;i++)>
{
s[i].nitin();
}
squeezable[] d=s;
lemon n[]=(lemon[])d; //line number 45.
}
}
hi everybody,
i am getting a classcast exception in line number 45.Please throw some light on it.

 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
g is an array of grapefruits. Then you created s and put g in it. It is still an array of grapefruits just in a citrus variable. Then you put s in a squeezable array d. It is still an array of grapefruits but now in a squeezeable variable.
Then you tried to cast the grapefruit array in d into a lemon. No, No, NO.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both lemon and grapefruit class extends citrus, so their peer classes and can not be casted to each other.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic