This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
vuthlarhi donald
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
source: Java beat Mock exams

class Fruit {}
class Apple extends Fruit {}
class Orange extends Fruit {}
Options :
a) List<? extends Fruit> stmt = new ArrayList<Fruit>();
b) List<? super Apple> stmt = new ArrayList<Fruit>();
c) List<? extends Fruit> stmt = new ArrayList<Apple>();
d) List<? super Orange> stmt = new ArrayList<Orange>();
e) All the above
f) None of these

the answer is e...just want more clarity when it comes to generics..interms of is-a and has a relationship's


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the question?
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This declaration:
List<? extends Fruit> stmt = new ArrayList<Apple>();
you can read as:
stmt is a List of <?> where ? must extend Fruit or ? must be Fruit
..... or in terms is-a: ? is-a Fruit

so assigment of new ArrayList<Apple> to stmt is correct, because ArrayList implements List (ArrayList is-a List),
and Apple extends Fruit (Apple is-a Fruit)

Similary for 'super' keyword:
List<? super Apple> stmt = new ArrayList<Fruit>();
you may read as:
stmt is a List of <?> where Apple must be superclass of ? or ? must be Apple
..... or int terms is-a: ? is-an Apple

so the declaration List<? super Apple> stmt = new ArrayList<Fruit>(); is correct, because ArrayList implements List,
and Fruit extends Apple (Apple is-a Fruit -or Apple is superclass of Fruit)

For example this declaration:
List<? super Fruit> stmt = new ArrayList<Apple>();
is wrong and gives compiler error, because Apple doesn't extend Fruit (Fruit is not superclass of Apple -or Apple is-not-a Fruit)
 
vuthlarhi donald
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much!!! you are a star
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) List<? extends Fruit> stmt = new ArrayList<Fruit>();

True, because Fruit extends Fruit. (in terms of ? extends Fruit)
b) List<? super Apple> stmt = new ArrayList<Fruit>();

True, because Apple is SUBCLASS of Fruit.
c) List<? extends Fruit> stmt = new ArrayList<Apple>();

True, because Apple extends Fruit
d) List<? super Orange> stmt = new ArrayList<Orange>();

True, because Orange is "SUBCLASS" of Orange (in terms of ? super Orange)


so the declaration List<? super Apple> stmt = new ArrayList<Fruit>(); is correct, because ArrayList implements List,
and Fruit extends Apple (Apple is-a Fruit -or Apple is superclass of Fruit)

This is wrong (bold part), the result is right. Typo???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic