• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question MindQ Mock exam

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
35. For the following code:
class Super
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class Runner
{ public static void main( String argv[] )
{ Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();
}
}
What will be printed to standard output?
a) The code will not compile.
b) The code compiles and "5, Super" is printed to standard output.
c) The code compiles and "5, Sub" is printed to standard output.
d) The code compiles and "2, Super" is printed to standard output.
e) The code compiles and "2, Sub" is printed to standard output.
f) The code compiles, but throws an exception.
In the above code, option "c" is given as the correct answer. Could you please explain why? I thought "e" would be the correct answer
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it prints "5" because you�re creating a Super object. But, this object is of the type Sub, and you have overriden the method printVal(), so the printVal() from Sub will be called. Please, anyone correct me if i�m wrong.
Francisco
[ June 05, 2002: Message edited by: Francisco A Guimaraes ]
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The type of the "object reference" determines which variable to access.
The type of the "underlying object" determines which method to invoke.
In the example,
Super sup =new Sub();
sup is defined as an object reference for class Super and the type of object it references is of Sub.

Hence,the o/p : 5,sub.
Am i correct?
[ June 05, 2002: Message edited by: geetha nagarajan ]
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But no instance of Super was created (only a reference to a Super object), and index in Super isn't static, so how would index get the value of 5?
[ June 05, 2002: Message edited by: Paul Villangca ]
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the type of the reference is Super, and the type of the reference determines which variable to access. So it prints 5
The type of the Object is sub, so the JVM checks if the method exist in Super (because the type of the reference is Super); if no Compiler error, if yes the JVM checks if the method exist in sub; if no the JVM use the method from Super, if yes the JVM use the method from Sub.
Since the method exist in Super and is overriden in Sub, the JVM use the one from Sub.
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I mean is:
class Super
{ int index = 5;
This is an instance initializer, right? So doesn't index get the value of 5 only when an instance of Super is created?
Super sup = new Sub();
There was no instance of Super created here, so how will index be 5?
I hope you guys can clear this up for me.
 
Francisco A Guimaraes
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from the JLS 15.11.1:

From this we can see that the important thing here is the type of the reference.
if you modify the code to this:


output: s.x=0 when s holds a class T at run time.
you still get the same output because the reference is still of type S.
Hope it helps,
Francisco
[ June 07, 2002: Message edited by: Francisco A Guimaraes ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Francisco,
Your explaination was excellent.........Thanks for that........
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi paul you had a good question ...
well i think the creation of the object state follows a sequence of steps which involve initialization of {static expressions , static blocks },{instance expressions, instance blocks} in the serialorder and then to the constructor and so on.
But when the case of creating the state of a derived object comes first the base class's state is created and then the derived class state is created.
Hope this helps
 
natasha Seth
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot everyone for the explanation. I think I get it now.
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation. Btw, I don't think I've seen this in R&H, so will this show up in the exam?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic