aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class cast Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Class cast" Watch "Class cast" New topic
Author

Class cast

vishnu hiranamayee
Greenhorn

Joined: Feb 23, 2006
Posts: 17
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
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39



"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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Think: Is the variable abs really a reference to an instance of Example?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
viresh dolli
Greenhorn

Joined: Mar 02, 2006
Posts: 3
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

Joined: Mar 20, 2006
Posts: 31
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

Joined: Mar 20, 2006
Posts: 31
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

Joined: Mar 20, 2006
Posts: 31
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
 
subject: Class cast
 
Similar Threads
default constructor
simple........
How it is possible abstract class have an object
TYPE CAST
Casting problem