| Author |
Overriding and shadowing
|
Hrishikesh Yeshwant Alshi
Ranch Hand
Joined: Dec 06, 2010
Posts: 62
|
|
Hi Friends,
Source: new rule roundup game on JavaRanch.
I am little confused about the below question:
I know that the member variables, static variables and static methods are SHADOWED and not OVERRIDDEN. But non-static methods are OVERRIDDEN.
Having said that, I still feel that the output should be "Killing" but I KNOW (by practice) that it is gonna be "Easy". I feel I am not absolutely clear about this kind of questions.
I tend to think in below lines:
1. Method dispaly() in the class Exam is protected so it will be inherited to SCJPExam class.
2. When the line scjpExam.display(); is executed it will call this method. (Which is not OVERRIDDEN but INHERITED.)
3. indirectly the call System.out.println(difficultyLevel); == System.out.println(this.difficultyLevel); this refering to the SCJPExam's instance.
4. Therefore, this.difficultyLevel should NOT print the SHADOWED value.
5. Therefore, the output should be "Killing".
Could anyone please point-out where exactly I am going wrong. Many thanks in advance.
|
Write your code as if the person who maintains it is a homicidal maniac who knows where you live.
OCPJP6/SCJP6 86%
|
 |
Thakur Sachin Singh
Ranch Hand
Joined: Jun 15, 2010
Posts: 209
|
|
|
you are right here...read again what you say.
|
my blog SCJP 6- 91%, IBM DB2, IBM RAD Certified
|
 |
Hrishikesh Yeshwant Alshi
Ranch Hand
Joined: Dec 06, 2010
Posts: 62
|
|
But the actual output is NOT "Killing" it is "Easy". Based on my thought process, it should be "Killing".
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Keep in mind that inherited doesn't mean that the sub class gets a copy of the method, it calls the super's version of the method.
And in the Exam class -- the display() method is part of that class; compiled with the class; and from the point of that method, the "this" reference is of type Exam. And since polymorphism doesn't apply to instance variables, it will get the Exam version.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Hrishikesh Yeshwant Alshi
Ranch Hand
Joined: Dec 06, 2010
Posts: 62
|
|
Thanks Henry.
So the mistake was in the striked out line.
1. Method dispaly() in the class Exam is protected so it will be inherited to SCJPExam class.
2. When the line scjpExam.display(); is executed it will call this method. (Which is not OVERRIDDEN but INHERITED.)
3. indirectly the call System.out.println(difficultyLevel); == System.out.println(this.difficultyLevel); "this" refering to the SCJPExam's instance. "this" is refering to the Exam class's instance.
4. Therefore, the output should be "Easy".
Am I right? (Please see in the bold sentence.)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Hrishikesh Yeshwant Alshi wrote:
3. indirectly the call System.out.println(difficultyLevel); == System.out.println(this.difficultyLevel); "this" refering to the SCJPExam's instance. "this" is refering to the Exam class's instance.
Am I right? (Please see in the bold sentence.)
No. The "this" reference is referring to SCJPExam instance -- it is just that the type of the reference is of the Exam class. And since polymorphism doesn't apply to instance variables, the compiler use the reference type to bind the variable.
Henry
|
 |
Hrishikesh Yeshwant Alshi
Ranch Hand
Joined: Dec 06, 2010
Posts: 62
|
|
Henry, I am very sorry but reference is also of the type SCJPExam and not of type Exam.
Did you mean that because the method display() is inherited (no copy of the method is provided to the Exam class.) . In context of the display() method "this" is of the type Exam?
|
 |
Aditya Jha
Ranch Hand
Joined: Aug 25, 2003
Posts: 227
|
|
In context of the display() method "this" is of the type Exam?
The 'this' reference always points to the instance for which the (instance) method is called (in this case, SCJPExam). It's 'type', however, can be thought of the class in which the executing method actually lies (in this case, Exam).
The point to note is in Henry's statement -
polymorphism doesn't apply to instance variables
|
 |
Hrishikesh Yeshwant Alshi
Ranch Hand
Joined: Dec 06, 2010
Posts: 62
|
|
Many thanks.
I understood. Now the concept is crystal clear.
|
 |
Anchit Herredia
Greenhorn
Joined: Jul 15, 2009
Posts: 26
|
|
Henry Wong wrote:
Keep in mind that inherited doesn't mean that the sub class gets a copy of the method, it calls the super's version of the method.
And in the Exam class -- the display() method is part of that class; compiled with the class; and from the point of that method, the "this" reference is of type Exam. And since polymorphism doesn't apply to instance variables, it will get the Exam version.
Henry
An excellent answer! This made the concept quite clear to me.
Now if what you say is true then it implies that if I overide the display() method in Scjp class and run the same program again then the output should be "Killing".
This is because:
-> calling display() will actually now call this.display()
-> since this.display() has been compiled with scjp class it will call this.difficultLevel
-> meaning that this.difficultyLevel should be printed.
I'll verify this once I reach home and have access to my pc. I'll post my findings in this thread. hehe
|
 |
 |
|
|
subject: Overriding and shadowing
|
|
|