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?
Adon't have amsgmethod, it is only declare in the subclass. So ifAcan be abstract, add an abstract method, if not, add an empty method (not good in the first place...). Or renameshowinAto match the methods name inBandC(if they should be the same)interface M{void msg();}which will be implemeted byBandC, so no generics required.T extends A,Twill only "see" the methods ofA, if the interface is only implemented inBandC, this won't work. It need to be implemented inA.