-1

I am trying to have a deep understanding on when a generic method is to be written versus a non-generic method. Both the Collection interface as well as the Collections helper class has the addAll() method albeit with different signatures and the former one is not a generic method whereas the later one is a generic method:

The signature of addAll() in Collection:

  boolean addAll(Collection<? extends E> c);

The signature of addAll() in Collections:

 @SafeVarargs
    public static <T> boolean addAll(Collection<? super T> c, T... elements){}

So as a general rule when is it appropriate to write a generic vs a non-generic method?

Edit: My understanding of the term generic method is that it will have a type parameter or comma separated list of type parameters in angle brackets before the return type of the the method. Correct me if the definition is not correct.

Geek
  • 26,489
  • 43
  • 149
  • 227
  • Both are generic methods, what's wrong? – Kayaman Aug 10 '14 at 19:52
  • @Kayaman He meant that the former is a method of a generic class whereas the lateer is simply a gernric method – Kumar Abhinav Aug 10 '14 at 19:53
  • The former is a generic **instance** method, the latter is a generic **static** method. Both methods are generic. – Kayaman Aug 10 '14 at 19:54
  • @Kayaman Look at the signature, the stuff in angle bracket before the return type is missing in the first snippet. – Geek Aug 10 '14 at 19:54
  • 1
    Yes, a static method needs the extra parameter, because it doesn't have the type from the enclosing class (like an instance method has). – Kayaman Aug 10 '14 at 19:56
  • possible duplicate of [What is the difference between bounded wildcard and type parameters?](http://stackoverflow.com/questions/1750273/what-is-the-difference-between-bounded-wildcard-and-type-parameters) – Adrian Cox Aug 10 '14 at 20:45

2 Answers2

0

I think that you should use only generic methods.If you use generic methods/containers your code is type safe. Collection and Collections classes keep some "non-generic" methods only to provide backwards compability. You could read "Effective Java - second edition"(written by Joshua Bloch), item 23 "Don’t use raw types in new code" and you will understand why it is better to use generic methods.

lucian.marcuta
  • 1,250
  • 1
  • 16
  • 29
0

First of all ,you need to understand that Collections is simply a concrete class with static methods for manipulating Collection interface instances.Therefore,the class Collections is not generic but it has a number of generic methods to tackle the parameters of static methods which may themselves be generic

Practically ,speaking both are generic methods.The former one is a generic method of a generic class whereas the latter is simply a generic method without the class being generic as a whole.

The super keyword in the argument of the 2nd case is simply to allow elements to be added to it.If it had been a collection of any kind which extends a particular type,then you would not have been able to add an element

Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35