• 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 Doubt

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The above question is from the SCJP Mock test with SCJP.5 K&B:
The correct answers are A and F

I am not clear why C ,D and E are not correct, please help
 
Biju Gopinathan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, the image is not properly attached, The question is as follows:

Given that CharSequence is an interface implemented by both String and StringBuilder classes, and then given the following method:

public <S extends CharSequence> S foo(S s){
//INSERT HERE
}

Which of the following can be inserted at //INSERT HERE to compile and run without error?

A return s;

B return (Object)s;

C return s.toString();

D return new StringBuilder(s);

E return (S)new StringBuilder(s);

F return null;
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi biju,

in this question A an F are correct.
B isn't correct because you cannot assign Object to CharSequence
or its subclasses.
C not correct, suppose that c is correct and foo return a String so if you call foo like this:

you have a compile-time error.
D like C just replace StringBuilder by String.
E Casting problem

hope this help you

khaled
 
Biju Gopinathan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not clear, with the above explanation

In the above methos the condition is that the return type should be a sub class of CharSequence, and both String and StringBuilder are sub class of CharSequence. So why can't we return a String or a StringBuilder instance.
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may be useful to think of the method call in this case as a template.

In this example, the method has to return the same type that it is called with.

If you were to insert the example that returns a string, but call the method with a StringBuilder, the method would be invalid:



[ May 20, 2008: Message edited by: Stevi Deter ]
reply
    Bookmark Topic Watch Topic
  • New Topic