aspose file tools*
The moose likes Beginning Java and the fly likes Declarations and scoping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declarations and scoping" Watch "Declarations and scoping" New topic
Author

Declarations and scoping

Curtis Rutter
Greenhorn

Joined: Sep 17, 2010
Posts: 3
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!


I never learned from a man who agreed with me.--Robert A. Heinlein
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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).



Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Curtis Rutter
Greenhorn

Joined: Sep 17, 2010
Posts: 3
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

Joined: Sep 14, 2009
Posts: 1732
    
  12

Curtis,

Welcome to Java Ranch!!

Glad you got a response so quickly!


When you do things right, people won't be sure you've done anything at all.
K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
this was simple but still useful post....

thanks Rowe, Curtis


“The difference between 'involvement' and 'commitment' is like an eggs-and-ham breakfast: the chicken was 'involved' - the pig was 'committed'.”
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
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

Joined: Jul 21, 2010
Posts: 6
sheriff is intelligent
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
sharma pankaj wrote:sheriff is intelligent
Flattery will get you anywhere
Curtis Rutter
Greenhorn

Joined: Sep 17, 2010
Posts: 3
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

Joined: Oct 13, 2005
Posts: 40027
    
  28
You're welcome but it was "GR" who provided 99% of the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declarations and scoping