The Base class will receive an unknown data structure via Data type. The Derived class needs to override the printObj method in order to print the content of the Data type object. For this example, the Data type object contains one field which is a string. How to override the printObj() to print the content of String message variable?
// generic class
public abstract class Base <T>{
T obj;
public Base(T t){
obj = t;
}
public T getObject(){
return obj;
}
public void setObject(T t){
obj = t;
}
public abstract void printObj();
}
// specific class
public class Derived<T> extends Base<T>{
public Derived(T t) {
super(t);
}
@Override
public void printObj() {
//?????
}
}
// data can be changed
public class Data {
String message;
public Data(String msg){
message = msg;
}
}
// application class
public class App {
public static void main(String[] args) {
Data d = new Data("Hello");
Derived<Data> dClass = new Derived<Data>(d);
dClass.printObj();
}
}
Derived<int[]> dClass2 = new Derived<int[]>(new int[] {1, 2, 3, 4, 5}); dClass2.printObj();?