• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the question?
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much!!! you are a star
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
reply
    Bookmark Topic Watch Topic
  • New Topic