aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Posts: 76
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

Joined: Sep 29, 2008
Posts: 12617

What's the question?
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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

Joined: Jul 31, 2006
Posts: 76
thank you very much!!! you are a star
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
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???

SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics