• 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

Shadowing attribute of superclass in subclass

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The following program prints "Easy". I do not understand this. Per my understanding, the "indivisible" overriding method in subclas should print value of te variable from subclass object and not from super class. Someone please exaplain:



TIA!
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all the variable defined as "difficultyLevel" is having the value as "Easy" in both ur super class and sub class. So how did u differentiate between the outputs?

Anyways, let me explain u:
1) Always remember that the variables(instance or static) are never inherited. They are hidden .

In this example of urs, the variable difficultyLevel called within the class Exam belongs to the class Exam and the one defined within the class SCJPExam belongs to the SCJPExam.

The invoked method printDifficultyLevel() present in the class Exam uses the variable "difficultyLevel" which is hidden from the same variable declared in the class SCJPExam, right?
So u get the kind of output mentioned.
 
Anand Wadhwani
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Animesh, your answers always help!

On this question, I still have doubts! First let me correct the error in above sample program. The value of shadowed varibale in subclass SCJPExam is "difficult". Now still program is printing Easy. Now here is my understanding about execution of the program.

1. Upon invokation of exam.printDifficultyLevel(); it looks for the method in SCJPExam class which not found, but found invisible inherited one from super class.

2. It executes the inherited version of method (invisible in subclass) in contect of "exam" object.

3. In cotext of "exam" object System.out.println(difficultyLevel); should print "Difficult", but it is printing "Easy".

Please suggest where am I wrong in above interpreatation.

Thanks for your time!
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Animesh and Anand,
I think it's more correct to say that variables (fields) *can* be inherited. But they are shadowed and not overridden, exactly. Note the following additions to Anand's code (a variable added to the superclass and a new method and new method call in the subclass):

Originally posted by Anand Wadhwani:




The new method call, exam.printDifficultyLevel2(), will print "MODERATE", the value of the difficultyLevel2 variable in the superclass, which was inherited.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From pete,


I think it's more correct to say that variables (fields) *can* be inherited. But they are shadowed and not overridden, exactly. Note the following additions to Anand's code (a variable added to the superclass and a new method and new method call in the subclass



No i dont agree.
Variables are only inherited if they are not hidden. They cannot be hidden and inherited at the same time.
In ur code, the variable difficultyLevel2 is inherited in the class SCJPExam , so u get the output as given. BUT suppose u had declared the same field in the sub class SCJPExam2 as well, sub class's variable would have hidden the super class variable.

If this was not the case how would the "difficultyLevel" would be accessed in the sub class. It would have been ambiguous, right? But actually its hidden. So for the same variable, in subclass scope u only see subclass variable and in super class scope u see super class variable.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Anand,


1. Upon invokation of exam.printDifficultyLevel(); it looks for the method in SCJPExam class which not found, but found invisible inherited one from super class.
2. It executes the inherited version of method (invisible in subclass) in contect of "exam" object


1) No actually in technical terms we dont say it this way. When a sub class inherits a super class, all the accessible members of the super class also become members of sub class until its overridden. So in this case printDifficultyLevel() is a method inherited in subclass


3. In cotext of "exam" object System.out.println(difficultyLevel); should print "Difficult", but it is printing "Easy".


Here we need to look where the method is being called from, here its within class Exam, so u get the variable of Class Exam and not of SCJPExam.


Go thru this JLS link, its quite well explained:
hiding
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic