• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Practice Question: What is the output?

 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile it and see. Have you done that yet? If so, do you have a question about what was output?
 
SOh BH
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Corey,
My intent was let others learn what I learned just now.
Thanks
Barkat
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barkat
If we comment out line,
System.out.println(field1);
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.
Regards
Maulin
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Vad.
I just left that compiler error in there on purpose.
Thanks
Barkat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic