Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How can I casting from parent class to children class

807580Jun 22 2010 — edited Jun 22 2010
Dear,
Could someone help me to casting from parent class to children class.
I have class like this

class parent{
String name;
String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

class children extends parent{
String address;

public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public children() {
}
public children(parent p) {
//Do init super class here
}
}
//--------------------------------
In the constructor
public children(parent p) {
//Do init super class here
}

I like to init super class by object p (this is instance of parent class). The way to do is using:
public children(parent p) {
super.setId(p.getId());
super.setName(p.getName());
}
But I don't like this, because, for example I have parent class with over 30 proberties, it take time to do like that.
There are any way to use super operation to init parent class, for example super = p;

Could you show me the way.
Thanks alot
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 20 2010
Added on Jun 22 2010
2 comments
194 views