Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declarations and scoping

 
Curtis Rutter
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've looked for an answer to this question and haven't found anything. Hopefully I haven't missed a post somewhere.
Background: I'm studying for the SCJP at a technical school where, unfortunately, none of the instructors at my location are Java experts. I'm stumped by the answer to one of the questions about the following code snippet (commented lines are those I've added to get the code to compile, line numbers as in the test question):

1. class SuperClass {

// public class A {
2. public A getA() {
3. return new A();
4. }
// }
5. }
6. class SubClass extends SuperClass {

// public class B {
7. public B getA() {
8. return new B();
9. }
// }
10. }

The "correct" answer says: "Compilation will succeed if B extends A." Other, "incorrect" answers are that A. compilation will fail because of an error in line 3 and that B. compilation will fail because of an error in line 9.

Given that I had to add code to even get the original code to compile (and doing so without "B extending A"), any thoughts on how the code should look to get to the answer as stated? What concept are they trying to test here? NetBeans pukes on lines 2, 3, 7, & 8.

Thanks!
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You added the "wrong lines". I think you have to assume classes A and B exist. In that case the only way the code will compile is if B extends A (google java covariant return type).

 
Curtis Rutter
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, sometime you miss the forest for the trees.

Great answer, and quick. And the Google suggestion was a good idea that helped.

Thanks!

CR
 
Janeice DelVecchio
Saloon Keeper
Posts: 1802
12
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Curtis,

Welcome to Java Ranch!!

Glad you got a response so quickly!
 
K Abhijit
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this was simple but still useful post....

thanks Rowe, Curtis
 
Campbell Ritchie
Sheriff
Pie
Posts: 47289
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct answer is "Compilation will succeed if B extends A and you are compiling on Java5 or Java6".

Covariant return was introduced in Java5.
 
sharma pankaj
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sheriff is intelligent
 
Campbell Ritchie
Sheriff
Pie
Posts: 47289
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sharma pankaj wrote:sheriff is intelligent
Flattery will get you anywhere
 
Curtis Rutter
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like the forums on JavaRanch include lots of folks who are willing to help everyone, including greenhorns like me. I'm impressed with the JavaRanch community. I'm running Java5, BTW.
Muchas gracias. CR
 
Campbell Ritchie
Sheriff
Pie
Posts: 47289
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome but it was "GR" who provided 99% of the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic