File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shadowing attribute of superclass in subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shadowing attribute of superclass in subclass" Watch "Shadowing attribute of superclass in subclass" New topic
Author

Shadowing attribute of superclass in subclass

Anand Wadhwani
Ranch Hand

Joined: Mar 21, 2005
Posts: 151
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!


SCWCD 1.4<br />---------------------<br />Ability is what you're capable of. <br />Motivation determines what you do. <br />Attitude determines how well you do it.<br />---------------------
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Mar 21, 2005
Posts: 151
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!
Pete Knecht
Ranch Hand

Joined: Mar 30, 2005
Posts: 33
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

Joined: Jul 19, 2004
Posts: 298
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

Joined: Jul 19, 2004
Posts: 298
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
 
 
subject: Shadowing attribute of superclass in subclass