Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MindQ's question no. 35

 
susan george
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
The answer given is c :The code compiles and "5,Sub" is printed to standard output.But i think it's e. Could someone pls. tell me which is the correct answer and why?

 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Susan,
The answer is correct because you must remember that when dealing with class variables there is no concept of overridding. The subclass can only hide the variables if it has one with an identical name. What that means is that the reference type is used to identify which variable to use. This is the reverse of the override methodology where the object type is used to identify which method to call.
So, in your case, when the following line is executed
sub.index
the JVM says that sub is a reference type Super so we should use that class variable.
Regards,
Manfred.
 
susan george
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manfred,
But it's not sub.index in the code.It's sup.index.So what I understood from the code is the runtime type of the object 'sup' is class Sub.So why is it that when sup.index is called it's not printing the value 2 which is the index value in class Sub?And another thing is that when sup.printVal() is called,according to the author, it prints "Sub".I didn't quite understand how this works.Should the output be "2,Sub"?
Thanks again.
Susan
 
Lam Thai
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by susan george:
Hi Manfred,
But it's not sub.index in the code.It's sup.index.So what I understood from the code is the runtime type of the object 'sup' is class Sub.So why is it that when sup.index is called it's not printing the value 2 which is the index value in class Sub?And another thing is that when sup.printVal() is called,according to the author, it prints "Sub".I didn't quite understand how this works.Should the output be "2,Sub"?
Thanks again.
Susan

Hi Susan,
Superclass's method is overriden by subclass's method. But superclass's field is only shadowed/hidden by subclass's field. In other words, object type/reference dictates the method that is to be invoked. Class type/reference dictates the field.
Let's go over this again with a few lines of code:
Assume Sub is extends from Sup, if we have
Sub S1 = new Sub(); // object Sub, class Sub
then S1.printVal() will refer to printval() of Sub (i.e. method goes with object type). And S1.index will use 'index' of Sub's class (i.e. field goes with class type, in this case it is Sub).
If we have
Sup S1 = new Sub(); // object Sub, class Sup!!!
then S1.printVal() will refer to printval() of Sub (i.e method goes with object type). BUT S1.index will use 'index' of Sup's class (i.e field goes with class type, in this case it is Sup).
Well I did not say anything new but I hope you do see the different concepts between 'method overriden' and 'field hidden'.
Regards,
Lam

 
susan george
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Lam.I understood the concept now.
Susan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic