public class Service{
String serviceName;
//setter and getter
}
public class Version{
int VersionID;
//setter and getter
}
public void test(Object list){
//it shd print the obtained list
}
List< Service> list1; //Service is a Bean
List< Version> list2; //Version is a Bean
test(list1);
test(list2);
Now the test method shd print the obtained list – (i.e) If the list is of type Service ,then serviceName should be printed using its getter. If the list type is Version versionID should be printed.
Is it possible to achieve this without using Interface or abstract class?
@danLeon has the simplest idea so far (adding
toStringto the classes), assuming you have access toServiceandVersion.I’m not sure why you were thinking of reflection, but the only thing I can think of is that you want something that will work with any object that has a single attribute
Stringgetter, and then you would do something like this (crazy IMO, but it uses reflection):I haven’t compiled or tried, but that is approximately right. Definitely some additional exception handling is required