1

I have been researching towards the answer to a tricky question in my first year java course, specifically i need to make a program capable of both summing the elements in a generic list, and finding the highest and lowest in the list.

I am perfectly capable of completing both of these tasks individually, having T(the generic type) extend Number for summing, and Comparable for comparing. The problem occurs when I attempt to accomplish both of the tasks on the same generic type seeing as java is incapable of multiple inheritance.

In short, how could I accomplish both of these tasks while having the upper bound of T be the Number class?

Code snippets and advice are appreciated!

1 Answers1

5

T extends Number & Comparable<T> should do it. T extends Number & Comparable<? super T> is a bit more general.

Java does support "multiple inheritance of interface" (and perhaps some weirdo multiple inheritance of implementation" in Java SE 8). A class extends one other class and can implement many interface.

(Not sure if java.lang.Number is what you want for summation. You might want to introduce you own type along similar lines to Comparator.)

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • Thanks ever so much with that! Since we haven't worked with comparator at all, i will simply use the fact that the elements must all be Number(s) and take their doubleValue(); My prof isn't exactly the greatest at providing such info to us, so again thanks! – user1297003 Mar 28 '12 at 03:01