I have below super class.
public class SuperOne{
private String id;
private String name;
//public setter and getter methods
}
public class SubOne extends SuperOne{
//To Do : SubOne has no extra fields.
}
I have below method:
public SubOne getData(String id){
SuperOne data = someDao.getData(String id)
// Now we have result in data . Now i need to convert data into SubOne and return.
}
SuperOne.java has no constructor and is provided by third party which i cannot modify.But i can modify SubOne as it is my own. Please suggest me the best way to convert SuperOne into SubOne.
You can use SubOne
as a wrapper instead as a child.
class SubOne {
private SuperOne superOne;
public SubOne(SuperOne) {
this.superOne = superOne;
}
public String getId() {
return this.superOne.getId();
}
public String getName() {
return this.superOne.getName();
}
public String setId(String id) {
return this.superOne.setId(id);
}
public String setName(String name) {
return this.superOne.setName(name);
}
///////////////
public SubOne getData(String id){
SuperOne data = someDao.getData(String id)
return new SubOne(data);
}
If you insist on inherit SuperOne
you can do it like this.
class SubOne extends SuperOne {
public SubOne(SuperOne superOne) {
super.setId(superOne.getId());
super.setName(superOne.getName());
}