This discussion is archived
2 Replies Latest reply: Dec 3, 2010 3:39 AM by 800268 RSS

Generic method with different parameter types

786367 Newbie
Currently Being Moderated
Java 6.0 | Generic Methods

I am trying to implement generics to one of my custom class method, such that it accepts List of custom objects.
eg: List<MyClassOne> , List<MyClassTwo>

Generic method :
public <T> void calcSubTotal(List<T> listObjects, Class<T> type) {

for(T obj : listObjects) {
// I want to invoke a method in MyClassOne , MyClassTwo, MyClassThree using the obj reference.
// All my custom classes have the same method name.
//

}

}

I am invoking this geenric method like this : calcSubTotal(list , MyClassOne.class );

calcSubTotal(list , MyClassTwo.class );


How can I invoke methods / properties in My Custom classes using the generic type parameter ?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points