1

supposed we know that ViewGroup extends View.
Further we have a generic, parametrized class A<T extends View>

Question:
Why wont method C.add() accept new A<ViewGroup>() as parameter?
Shouldn't it work, because of polymorphism?

Class diagram

SOLUTION: Singning add with ? extends View lets add accept new A<ViewGroup>() as a parameter.

Solution

1

4 Answers 4

7

You signed your add method as:

static void add(A<View>)

but you probably meant:

static void add(A<? extends View> a)
Sign up to request clarification or add additional context in comments.

4 Comments

This still won't work because new A<ViewGroup>() is invalid.
@tskuzzy What do you mean? It's like new ArrayList<ViewGroup>().
The OP had a typo in the question. He originally said that View extended ViewGroup. :P
Thnx, signing add with add(A<? extends View> a) lets add() accept new A<ViewGroup>() thnx. Sorry for my initial typo.
1

Not quite because add() might be using a method of View that is not found in ViewGroup.

Summary: View is a ViewGroup, however ViewGroup is not a View. Polymorphism is where you can assign a View object to a ViewGroup declaration.

2 Comments

You said "we know that View extends ViewGroup". o_o
sorry ... of course ViewGroup extends View, like on the class diagram
1

First of all, your question isn't very clear because the UML diagram contradicts your text. You say that View extends ViewGroup, but the diagram shows the reverse : ViewGroup extends View.

Now, a List<Car> doesn't extend a List<Vehicle>. If it were the case, you could do:

List<Car> listOfCars = new ArrayList<Car>();
List<Vehicle> listOfVehicles = listOfCars;
listOfVehicles.add(new Bicycle());
// now the list of cars contain a bicycle. Not pretty. 

1 Comment

sorry, just edited the sentence, of course ViewGroup extends View here.
1

First of all you said View extends ViewGroup, but the diagram says ViewGroup extends View (which I assume to be right).

Secondly, you are not allowed to pass a List<ViewGroup> as a List<View>. This is a compile time protection to prevent someone from adding an AnotherView into this list and compromise type-safety of generics.

List<ViewGroup> is not a subtype of List<View>, but it is a subtype of List<? extends View>. So you can modify your method to accept a List<? extends View> instead, but be aware that you can't add to the list passed to the method this way.

There is also another syntax called lower bound wildcard (List<? super ViewGroup>), as opposed to the upper bound wildcard mentioned above, which enables you to add to the list but you can olny pass in a list of ViewGroup or its parents.

More about wildcards in generics can be found here: http://download.oracle.com/javase/tutorial/java/generics/wildcards.html

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.