aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics return type 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 return type" Watch "Generics return type" New topic
Author

Generics return type

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi, This is from K&B mock exam.
Given answer is although String and StringBuffer are both subtypes of CharSequence, they are not necessarily
the same subtype as whatever is passed in (S)..I am not clear. Please let me know


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

1) S extends CharSequence. It could be any class that extends CharSequence and not necessarily String. What happens if I call fooC(StringBuffer)? Java needs to return a StringBuffer, but the method has a String.

2) What happens if you try to do return (StringBuilder) s.toString(); without generics. Why doesn't it work?

3) Same reason as #1.

4) Same reason as #2.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

Yes, the given answer is correct.
Malte Wannerskog
Ranch Hand

Joined: Jan 06, 2011
Posts: 92

I have a question about this particular question aswell...

the line

compiles and runs just fine without any compiler warnings for me. Am I missing something?

Same goes for

No compiler warnings there aswell. Even though this was not an option in my question.

Thanks for your help


OCPJP (310-065) - 88%
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Welcome to JavaRanch, Malte Wannerskog !


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Malte Wannerskog
Ranch Hand

Joined: Jan 06, 2011
Posts: 92

Raghavan Muthu wrote:Welcome to JavaRanch, Malte Wannerskog !


Thank you, been reading posts here for a while but thought i would be a member now since Im studying for the SCJP exam

Any ideas on my question?
Malte Wannerskog
Ranch Hand

Joined: Jan 06, 2011
Posts: 92

Malte Wannerskog wrote:I have a question about this particular question aswell...

the line

compiles and runs just fine without any compiler warnings for me. Am I missing something?

Same goes for

No compiler warnings there aswell. Even though this was not an option in my question.

Thanks for your help


Ah nevermind, I realized that the compiler error didnt occur until you actually try to use the method .
For example with
michael schroeder
Greenhorn

Joined: Feb 23, 2011
Posts: 1
Hmm, now I think I am missing something:



This program compiles with warnings, but runs fine. This is part of the mock questions of the SCJP Java 6 Study Guide for 310-065 (Quiz #56). The given answer to the question is, that both return statements will not compile or they will not run without errors. But the compiler thinks different about that.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics return type