File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes java.lang.ClassCastException: Parent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.lang.ClassCastException: Parent" Watch "java.lang.ClassCastException: Parent" New topic
Author

java.lang.ClassCastException: Parent

Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
Why this code is giving this exception :java.lang.ClassCastException: Parent



class Parent {
void dis(){
System.out.println("parent");
}
}
public class Child extends Parent{
void dis(){
System.out.println("child");
}
void dis1(){
System.out.println("child");
}
public static void main(String[] args) {
Parent p1=new Parent();
Child c1=new Child();
c1=(Child)p1;
c1.dis();
}
}
Chandra Sekhar
Ranch Hand

Joined: Sep 26, 2003
Posts: 90
Hi,

You cannot cast a "Child" object to its "Parent" like this...
Though it will compile ,on runtime java identifies that p1 holds a reference which is not assignment compatiable with c1 and will throw java.lang.ClassCastException: Parent


But the reverse you can do

Parent p1=new Parent();
Child c1=new Child();
p1=c1;



Chandrasekhar
[ September 28, 2004: Message edited by: Chandra Sekhar ]

"Luck is when opportunity meets preparation"
Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
Thanks chandra.

I am still confused ..

At compilation time the child reference points to the parent object after typecasting.. then wats the problem at runtime...

Akshay.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
yeah wiered. could you paste the whole StackTrace??
Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
Exception in thread "main" java.lang.ClassCastException: Parent
at Child.main(Child.java:16)

Akshay.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
ok try this. make your parent class public as well. then get back to us.
Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
ok try this. make your parent class public as well. then get back to us



How can we have 2 public classes in same source file..

code is

class Parent {
void dis(){
System.out.println("parent");
}
}
public class Child extends Parent{
void dis(){
System.out.println("child");
}
void dis1(){
System.out.println("child");
}
public static void main(String[] args) {
Parent p1=new Parent();
Child c1=new Child();
c1=(Child)p1;
c1.dis();
}
}
Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
Sorry for posting the code again
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874


really dont know that.

make it two buddy.
Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
could not get you adeel.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Akshay,

The p1, referencing to a Parent Object, cannot be cast to Child because a Parent is not a Child. But a Child is a Parent since the Child inherits all the methods in Parent class. However, if p1 is referencing to a Child object or a subclass of Child, then you can cast p1 to Child.

For example:


You can also refer to Java Specification: 5.5 Casting Conversion for more info.

Joyce
[ September 28, 2004: Message edited by: Joyce Lee ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
the problem is not that.

as we can pass java.sql.Date to java.util.Date, without any casting.

java.util.Date d = new java.sql.Date();

and we can do vice versa by add casting like,

java.sql.Date d = (java.sql.Date)new java.util.Date();


because sql.Date is a sub-class of util.Date. then why not this,

Child c = (Child)new Parent(); // Child is a sub-class of Parent
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi adeel,



I received "java.lang.ClassCastException" for the above line.

Joyce
[ September 28, 2004: Message edited by: Joyce Lee ]
Chandra Sekhar
Ranch Hand

Joined: Sep 26, 2003
Posts: 90
Originally posted by adeel ansari:
the problem is not that.
as we can pass java.sql.Date to java.util.Date, without any casting.
java.util.Date d = new java.sql.Date();
and we can do vice versa by add casting like,
java.sql.Date d = (java.sql.Date)new java.util.Date();
because sql.Date is a sub-class of util.Date. then why not this,
Child c = (Child)new Parent(); // Child is a sub-class of Parent



Hi,
Did u ty to compile and run this..??
java.sql.Date d = (java.sql.Date)new java.util.Date();
This will give u the same ClassCast Exception at run time.

Without the cast to Child you would get a compile time error. The cast tells the compiler that you really mean to do this and the actual type of Parent does not get resolved until runtime. Casting down the object hierarchy is a problem, as the compiler cannot be sure what has been implemented in descendent classes. Casting up is not a problem because sub classes will have the features of the base classes. This can feel counter intuitive if you are aware that with primitives casting is allowed for widening operations (ie byte to int).

Plz check http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#175725

Chandrasekhar
SCJP
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Joyce Lee:

The p1, referencing to a Parent Object, cannot be cast to Child because a Parent is not a Child.



would it work?

Vector vtr = (Vector) new Object();
[ September 28, 2004: Message edited by: adeel ansari ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
yeah it wouldn't work. sorry mates a bit disturbed.
thats why sometimes i get into beginners forum. it is really worthy.
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
would it work?

Vector vtr = new Object();



It not work,

Type mismatch, It can not convert from Object to Vector.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
thnx everybody.

Akshay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.ClassCastException: Parent
 
Similar Threads
static method confusion
Access control
Inheritance and static methods
Problem -- Virtual Method Invocation
Question on Overriding