I need to create generic list that can act as List<ClassA> or List<ClassB> because I have following situation.
if(e instanceof A)
return A;
else
return B
But I want to populate one List Result which can act as either List<ClassA> Result or List<ClassB> Result, am sure we can use generics here but i am not sure how to use it?
Update
All i want to do is that runtime, my List should be populated with proper type, either class A or class B,
if()
{
//data is present in List<ClassA> classAList = new ArrayList<ClassA>();
//return classAList
}
else
{
//return List<ClassB> classBList = new ArrayList<ClassB>();
}
Hope this helps.
ClassAandClassBrelated to each other, as in they share the same interface or parent class?List<Object>which encompasses both, then useinstanceofto determine the type of each.