У меня есть следующие классы:
public class Base{
//fields
public String getStr(){
String str = null;
//Getting str from the fields
return str;
}
}
public class Derived extends Base{
//fields
//some other fileds
public String getStr(){
String str = null;
//Getting str from the fields + some other fields
return str;
}
}
Теперь у меня есть метод, который имеет параметр типа Derived
.
public void makeStr(Derived d){
Base b = null;
//Getting the Base class subobject from d and printing the str
}
Но я не могу просто выполнить назначение как b = d;
, а затем вызвать b.getStr()
, потому что будет вызван метод d.getStr()
.
Как создать объект типа Base
из подобъекта Base
объекта objec типа Derived
? Я факт, я просто хочу создать копию субъекта Base
типа Derived
.