Call method using type variable generics

Viewed 88

I have a scenario that i want to create one support class called D which contains a generic method. I have set the upper bound for type variable.

class A{
    void show(){
        System.out.println("Hello A");
    }
}

class B extends A{
    void msg(){
        System.out.println("Hello B");
    }
}

class C extends A{
    void msg(){
        System.out.println("Hello C");
    }
}

class D{
    <T extends A> void display(T ob){
        ob.msg(); //here i want to do some tricks
    }
}

First i want to share my objective. Here msg() function of B and C class has different implementations. I want to create one support class called D that has one display method, using display method i want to call either msg() function of B or C class dependent on instantiation. Can you tell me how can i achieve it?

3 Answers
Related