File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class cast

 
vishnu hiranamayee
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • 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");
}
}
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think: Is the variable abs really a reference to an instance of Example?
 
viresh dolli
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
rajeshwar Rao
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic