W
W
WTFAYD2016-08-11 17:57:53
Java
WTFAYD, 2016-08-11 17:57:53

Is the supertype metacharacter sentence in the textbook correct?

Hello!
Quote from the textbook (English and Russian version, Thinking in Java):

The argument apples is a List of some type that is the base type of Apple; thus you know that it is safe to add an Apple or a subtype of Apple.

The apples argument is a List container for some type that is Apple's base type; it follows that Apple and Apple-derived types can be safely included in a container.

public class SuperTypeWildcards {
  static void writeTo(List<? super Apple> apples) {
  apples.add(new Apple());
  apples.add(new Jonathan());
  // apples.add(new Fruit()); // Error
}

I can't find the logic here. If apples is a List container for some type that is Apple 's base type, then why can't you add Fruit to the container, but you can add, for example, Jonathan , which is NOT Apple 's base type , but derived from it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton, 2016-08-11
@WTFAYD

Read this link and this

R
Roman, 2016-08-11
@r_zaycev

The apples argument is a List container for some type whose base is Apple; it follows that Apple and Apple-derived types can be safely included in a container.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question