• 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

Class cast

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
code below is giving me error :
java.lang.ClassCastException
at Example.main(Example.java:28)
Exception in thread "main"


class Abs
{
void m()
{
System.out.println("hellooo11111111");
}
}

public class Example extends Abs {

public static void main(String args[])
{
Example a = new Example();
Abs abs = new Abs();
Example a1 = (Example) abs; // why is this giving error?
a1.m(); //can't we cast like this.



}
void m()
{
System.out.println("hellooo");
}
}
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"abs" is not an instance of Example -- it is an instance of its super class. You can't cast something to something that it is not.

Henry
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think: Is the variable abs really a reference to an instance of Example?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Abs
{
void m()
{
System.out.println("hellooo11111111");
}
}


public class Example extends Abs {

public static void main(String args[])
{
Example a = new Example();
Abs abs = new Abs();
//Example a1 = (Example) abs; // why is this giving error?
abs=a;
//a1.m(); //can't we cast like this.
abs.m();


}
void m()
{
System.out.println("hellooo");
}
}

this will work fine...it may help u
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi every one this is my first day. I am planning to write SCJP1.4 by the end of may or in first week of june.

I heard about this forum and it brings very much confidence in me i can do well in the exam if i participate and learn from this forum.

I hope evey one helps me.
 
rajeshwar Rao
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi every one this is my first day. I am planning to write SCJP1.4 by the end of may or in first week of june.

I heard about this forum and it brings very much confidence in me i can do well in the exam if i participate and learn from this forum.

I hope evey one helps me.

I feel we can even do the below way to work properly

class Abs
{
void m()
{
System.out.println("hellooo11111111");
}
}


public class Example extends Abs {

public static void main(String args[])
{
Example a = new Example();
Abs abs = new Abs();
//Example a1 = (Example) abs; // why is this giving error?
abs=a;
//a1.m(); //can't we cast like this.
abs.m();


}
void m()
{
System.out.println("hellooo");
}
}


Please correct me if i am wrong or if i am misleading from the actual concept.
 
rajeshwar Rao
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am very sorry. I pasted the wrong code. I was little bit confused.

This is the right code:


class Abs
{
void m()
{
System.out.println("hellooo11111111");
}
}


public class Example extends Abs {

public static void main(String args[])
{
Example a = new Example();
Abs abs = new Abs();
Abs ab;
//Example a1 = (Example) abs; // why is this giving error?
ab=a;
//a1.m(); //can't we cast like this.
ab.m();
ab=abs;
ab.m();



}
void m()
{
System.out.println("hellooo");
}
}
 
I want my playground back. Here, I'll give you this tiny ad for it:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic