Q:

Using Java generics instead of reflection to access common functionality of subclassed objects

I am trying to figure out if generics would be a better way to do what I'm trying to accomplish below (or some other way) instead of using reflection (which I currently have working just fine, but don't like it...). 
I have a class library I'm using that is roughly like the following:
abstract class base<T>{
    public boolean method1 (String who) {
        System.out.println(who+":s");
        return true;
    }
    public T method2 (String who) {
        System.out.println(who+":d");
        // The following gives me an unchecked cast warning.  I can't figure out
        // the proper way to fix that either, but it works, so I've moved on...
        // In the main program, it's this:      obj = (T) in.readObject();
        // basically deserializing the object from disk.  

        return (T) new Object();    
    }

    public abstract void load();
}

class Api1 extends base{
    @Override
    public void load(){
        System.out.println("api1:load()");
    }
}

class Api2 extends base{
    @Override
    public void load(){
        System.out.println("api2:load()");
    }
}

I am trying to wrap the above class library using the code below... This is what I cannot figure out how to do.  The objects that are passed to init() have the same methods (method1(), method2() and load()).  I don't want to write init() for each class type, instead, I just want to write it once, then have the compiler handle the details. 
UPDATE: There are currently 6 classes that are derived from Base (with a few more likely).  In the wrapper, I'm just trying to simplify access to the underlying APIs by handling various housekeeping chores automatically.  In the init() method, I need to be able to call the methods of those underlying objects (based on Api1, Api2, ...), without using reflection, or without writing duplicate code for each class, or getting a bunch of unchecked warnings and/or casting...
class ApiWrapper<T>{

    // would like to return the type passed in if that's doable
    // e.g. public T init(T t)

    public void init(T t){

        System.out.println("inside init: "+t.getClass().toString());

        //t.method1(t.getClass().toString());
        //t.method2(t.getClass().toString());
        //t.load();
    }
}

class api1Wrap extends ApiWrapper {
    Api1 api = new Api1();

    public api1Wrap(){
        init(api);
    }
}

class api2Wrap extends ApiWrapper {
    Api2 api = new Api2();
    public api2Wrap(){

        init(api);
    }
}

public class GenericApiWrapper {
    private final api1Wrap api1;
    private final api2Wrap api2;

    public GenericApiWrapper() {
        this.api1 = new api1Wrap();
        this.api2 = new api2Wrap();
    }
}

From a main() somewhere ...
GenericApiWrapper gaw = new GenericApiWrapper();

It feels to me like generics are the right approach for this type of thing, but I've been trying all sorts of combinations, and reviewing various articles and examples, but I just can't seem to figure out the proper approach.  If this has already been asked and answered, please point me to it.  I can't seem to find it, probably because I'm not sure how to describe what I'm trying to do. :)  I've been looking at the Java Generics FAQ as well, but I haven't found what I'm looking for... Thanks in advance! 

A:

In generics you should specify the type you want to use, in this case you replace the T with for example Api1 otherwise the compiler uses Object:
class api1Wrap extends ApiWrapper<Api1> {
   Api1 api = new Api1();

   public api1Wrap(){
      init(api);
   }
}

Also you could improve your code by simple implementing one single class and creating several instances with different types, this is the idea behind generics.  
UPDATE: 
If you also specify that T extends base, then the compiler is able to find the methods. 
Take a look at this:
class ApiWrapper<K, T extends base<K>>{

    private T t;

    public ApiWrapper(T t){
        this.t = t;
    }

    public void wrap(){
        init(t)
    }

    private void init(T t){
        t.method1("hello");
        K k = t.method2("world");
    }
}

ApiWrapper<Object, Api1> apiWrapper1 = new ApiWrapper<>(new Api1());

