• 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

java.lang.ClassCastException: Parent

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Akshay Bhatia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah wiered. could you paste the whole StackTrace??
 
Akshay Bhatia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.ClassCastException: Parent
at Child.main(Child.java:16)

Akshay.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok try this. make your parent class public as well. then get back to us.
 
Akshay Bhatia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for posting the code again
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


really dont know that.

make it two buddy.
 
Akshay Bhatia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could not get you adeel.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi adeel,



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

Joyce
[ September 28, 2004: Message edited by: Joyce Lee ]
 
Chandra Sekhar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah it wouldn't work. sorry mates a bit disturbed.
thats why sometimes i get into beginners forum. it is really worthy.
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

would it work?

Vector vtr = new Object();




It not work,

Type mismatch, It can not convert from Object to Vector.
 
Akshay Bhatia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thnx everybody.

Akshay
 
reply
    Bookmark Topic Watch Topic
  • New Topic