aspose file tools*
The moose likes Beginning Java and the fly likes Extend Test - ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extend Test - ClassCastException" Watch "Extend Test - ClassCastException" New topic
Author

Extend Test - ClassCastException

SAM KUMAR
Greenhorn

Joined: Jan 02, 2002
Posts: 25
Hi,
I have two class: one is base class and 2nd class extends the base class
BaseBean
ExtBaseBean extends BaseBean
Code Snippet:
BaseBean bean = new BaseBean();
ExtBaseBean eBean = (ExtBaseBean) bean;
eBean.setName("SK");
System.out.println("Name: " + eBean.getName());
I am getting ClassCastException..Any suggestion why I am getting ClassCastException exception.
Thanks in advance.
SK
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

An ExtBaseBean is a kind of BaseBean, but a BaseBean is not a kind of ExtBaseBean. This means you can treat an ExtBaseBean as a BaseBean, but you can't do the reverse. The line
BaseBean bean = new BaseBean();
creates an actual BaseBean object; then the line
ExtBaseBean eBean = (ExtBaseBean) bean;
tells the compiler "You're going to have to trust me; the variable "bean" points to an ExtBaseBean object." But it does not; when the JVM figures out that you "lied" to the compiler, you get the ClassCastException.
Make sense?


[Jess in Action][AskingGoodQuestions]
SAM KUMAR
Greenhorn

Joined: Jan 02, 2002
Posts: 25
Thanks Ernest for your help in understaning "Extend". But now I am stuck how to make clonable. Previously I was thinking of using following code:
BaseBean
--------
public Object clone() {
BaseBean copy = new BaseBean();
copy.member1 = member1;
copy.member2 = member2;
return copy;
}
ExtBaseBean
-----------
public Object clone() {
ExtBaseBean copy = (ExtBaseBean) super.clone();
copy.member3 = member3;
copy.member4 = member4;
return copy;
}
Obiviously I will get ClassCastException...but do you have any suggestions to make clonable. One alternative is to have a new method in BaseBean which will copy all the member variables:
BaseBean
--------
protected void copyAllMembers(BaseBean bean) {
bean.member1 = member1;
bean.member2 = member2;
}
public Object clone() {
BaseBean copy = new BaseBean();
copyAllMembers( copy );
return copy;
}
ExtBaseBean
-----------
public Object clone() {
ExtBaseBean copy = new ExtBaseBean();
copyAllMembers( copy );
copy.member3 = member3;
copy.member4 = member4;
return copy;
}
Any suggestion.
Thanks in advance.
SK
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi S Kumar
did you try it?
I don't think you will get ClassCastException as Clonning works differently then you seem to think.
You can get online free copy of "Thinking in Java" and look at the chapter about clonning.
Regards
Maulin
SAM KUMAR
Greenhorn

Joined: Jan 02, 2002
Posts: 25
Hi Molin:
I did try it and I know why I was getting ClassCastException because of following reason (Thanks to Ernest for explaining concept of Extend):
"An ExtBaseBean is a kind of BaseBean, but a BaseBean is not a kind of ExtBaseBean. This means you can treat an ExtBaseBean as a BaseBean, but you can't do the reverse."
Actually I was trying to simulate manual clone by having my own code to perform clone. I did look into TIJ and cloning was very simple to do. I added following code to BaseBean:
public Object clone() {
Object o = null;
try {
o = super.clone();
} catch (CloneNotSupportedException e) {
System.out.println("BaseBean.clone(): Not supported: " + e.getMessage() );
}
return o;
}
Now my question is that if somebody calls ExtBaseBean.clone() then whether it will return cloned object which has all the member data (variable) from BaseBean as well as ExtBaseBean (ExtBaseBean extends BaseBean) ?
Thanks in advance.
SK
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi S Kumar
Your second post that had that clone() thing is almost right but you have to modify it as follows,

hope this helps...
I would recommend to read TIJ's clonning in "details" until you are clear and experiment out things mentioned there. Myself took sometime in grasping things in clonning from TIJ and it was useful.
Regards
Maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extend Test - ClassCastException
 
Similar Threads
EJB Inheritance and lifecycle
Using inheritance for managed beans
Casting question
Please Help....NullPointerException...
Generating Dynamic XML using Reflection for Classes