Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding and shadowing

 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.



 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right here...read again what you say.
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the actual output is NOT "Killing" it is "Easy". Based on my thought process, it should be "Killing".
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks.

I understood. Now the concept is crystal clear.
 
Anchit Herredia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic