• 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

 
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: 23951
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");
}
}
 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic