Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Overriding and shadowing

 
Ranch Hand
Posts: 62
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Ranch Hand
Posts: 251
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right here...read again what you say.
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the actual output is NOT "Killing" it is "Easy". Based on my thought process, it should be "Killing".
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks.

I understood. Now the concept is crystal clear.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic