• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Declarations and scoping

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Curtis,

Welcome to Java Ranch!!

Glad you got a response so quickly!
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this was simple but still useful post....

thanks Rowe, Curtis
 
Marshal
Posts: 74606
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sheriff is intelligent
 
Campbell Ritchie
Marshal
Posts: 74606
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sharma pankaj wrote:sheriff is intelligent

Flattery will get you anywhere
 
Curtis Rutter
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74606
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome but it was "GR" who provided 99% of the answer.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic