Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

mixing Generics & non-generics

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. The following code is taken from the javabeat mock exams.

Given:

1. import java.util.*;
2. public class Fruits {
3. public static void main(String [] args) {
4. Set c1 = new TreeSet();
5. Set o1 = new TreeSet();
6. bite(c1);
7. bite(o1);
8. }
9. // insert code here
10. }
11. class Citrus { }
12. class Orange extends Citrus { }

Which, inserted independently at line 9, will compile? (Choose all that apply.)

A). public static void bite(Set<?> s) { }
B). public static void bite(Set<Object> s) { }
C). public static void bite(Set<Citrus> s) { }
D). public static void bite(Set<? super Citrus> s) { }
E). public static void bite(Set<? super Orange> s) { }
F). public static void bite(Set<? extends Citrus> s) { }
G). Because of other errors in the code, none of these will compile.


According to me A,B,C,D,E,F are all correct

But the answer mentioned is A,E,F are correct. Could anyone please clarify?
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by R Kumar:
According to me A,B,C,D,E,F are all correct

You are correct. Since o1 and c1 have no element type information, the compiler assumes that the programmer knows what he does, and issues only a warning (except for case A, where any list is valid).
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

You're absolutely right: for the given scenario the answers A,B,C,D,E,F are all correct.

If you'd start to insert code in method bite(), then you'd face limitations.
For example, change the bite()-method a little bit and add the command
s.add(new Citrus());

Now it becomes more interesting :-)

A. bite(Set<?> s) is NOT OK, because you could also pass a Set consisting of any type, i.e. also e.g. of String, thus you'd be not allowed to add a Citrus.

B. bite(Set<Object> s) is OK, because
you can add any subtype of Object to the set

C. bite(Set<Citrus> s) is OK, because
you can add any type of Citrus and any sub-type

D. bite(Set<? super Citrus> s) is OK, because
the Set must be of type Citrus or a super-type of Citrus, thus
it will be always possible to add a Citrus-object to the set.

E. bite(Set<? super Orange> s) is NOT OK, because
there could be e.g. an interface that is only implemented by class Orange,
thus it would not be allowed to add e.g. a Citrus.

F. bite(Set<? extends Citrus> s) is NOT OK, because
theoretically any type that extends Citrus is allowed, not only Citrus. One could pass a Set consisting of let's say Oranges, and then we couldn't longer add a Citrus-object.

Have fun playing around!
Robert
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic