wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Practice Question: What is the output? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Practice Question: What is the output?" Watch "Practice Question: What is the output?" New topic

Practice Question: What is the output?

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Compile it and see. Have you done that yet? If so, do you have a question about what was output?

SCJP Tipline, etc.

Joined: Oct 07, 2003
Posts: 6
Hi, i just compiled it, but there is compile time error, because field1 is undefined in super class, and i don't know where is compilcation in this code? its very simple. if u think there is anything tricky, please let us know.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Corey,
My intent was let others learn what I learned just now.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Barkat
If we comment out line,
from Super and try to run it then predict the output and see what you get. That is also little fun if we didn't know already the reason for the output.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Hi Barkat,

The code compiles when commenting out field1 in Super because Super doesn't see it. In the code, the type declarations are hidden in the child class, but no method is overridden, so polymorphism doesn't hold. It's all about static binding. So, in aMethod() we have:
this.new Inner() creates an object of Inner class defined in Super even though the currently running object this is of Test type. Same thing happens with new Inner_Static(); call. To print out the field1 variable, you can downcast this to Test. Would you agree?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Yes Vad.
I just left that compiler error in there on purpose.
I agree. Here's the link: http://aspose.com/file-tools
subject: Practice Question: What is the output?
Similar Threads
question from Dan's exam - ClassCastException
Need help explaining some results
Access modifiers
inner class query
practice question: predict the result