2

What is happening in line 6? <C extends Cat> is the return type of useMe, right? What does <? super Dog> do?

2. class Animal { }
3. class Dog extends Animal { }
4. class Cat extends Animal { }
5. public class Mixer<A extends Animal> {
6. public <C extends Cat> Mixer<? super Dog> useMe(A a, C c) {
7. //Some code
8. } }    
1
  • I think useMe is your method. if this is the case then you are wrong at <C extends Cat> Mixer<? super Dog>. Commented Jan 22, 2012 at 11:48

4 Answers 4

3

The <C extends Cat> specifies that useMe has one generic parameter, C, which must extend Cat.

Its return type is Mixer<? super Dog>. The ? denotes a wildcard.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Can you tell me how to use more than one generic parameters here? Separating by comma is not working. (like <C extends Cat>, <D extends Dog>)
3

No, the return type is Mixer<? super Dog>, and the method itself is a generic method which uses a generic parameter C, which can any class that extends Cat, and is used as a parameter C c

Comments

3

The first generic parameter specification <C extends Cat> makes useMe a generic method parametrized with parameter C which derives from Cat or is Cat itself.

The second generic parameter specification <? super Dog> refers to the method's return type which is a parametrized Mixer where the sole generic parameter is a super class of Dog or Dog class itself.

Thus, line 6 means: useMe is a generic method parametrized with C deriving from Cat (or being Cat itself). The method takes two arguments of types A and C and returns type Mixer parametrized with a super-type of Dog (possibly Dog itself).

Comments

2

<C extends Cat> is NOT the return type. Mixer<? super Dog> is. The former is only specified to specify the type of c.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.