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 hidding of member var Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hidding of member var" Watch "hidding of member var" New topic
Author

hidding of member var

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
what can be done to this code to print 2?
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
hi:
you need to override method <void x()> in class Test.


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Muzammil Khan
Greenhorn

Joined: Jul 22, 2003
Posts: 8
Hi Barkat Mardhani ,
I agree with Hanna Habashy.
When the compiler compiles new Test().x();.
It first looks for the method x in class Test.
If it does not find the method x it looks for the method x in the immediate superclass, which in this case is the class called Test1.
The method x in class Test1 executes the the code System.out.println(x);
The value of the variable x in class Test1 is 1. Hence, 1 gets printed.
what can be done to this code to print 2?
Solution :
Override the method in class Test, your code should look like this

In this scenario the value printed will be 2, because the value of x in class Test1 is 2.
I hope this helps.
Muzz
MSC Computing
NOTHING WORTH WHILE WILL EVER BE ACHIEVED WITHOUT DEEP THOUGHT AND HARDWORK - J.R.D TATA


Muzz<br />MSC Computing<br />Nothing worth while will ever be achieved without DEEP THOUGHT AND HARDWORK - J.R.D TATA
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch Muzz.
_______________________________________________________________
It is also feasible to use the following cast:
((Test)this).x


SCJP2. Please Indent your code using UBB Code
Lalitha Chandran
Ranch Hand

Joined: Jul 03, 2003
Posts: 92
Hi
I can deduce the following from this code.
If you have a method in base class which is not overridden but it uses a variable which is hidden in the child class, then the method(if invoked on a child object) uses the variable in the base class and not the one in child class.
Please let me know if I am correct.
Thank you
Lalitha
Lalitha Chandran
Ranch Hand

Joined: Jul 03, 2003
Posts: 92
Hi Jose
You are very correct we do get the desired output.


_______________________________________________________________

It is also feasible to use the following cast:
((Test)this).x

But I think we shouldn't follow this approach because in this code
class Test1 {
public int x = 1 ;
void x() {
System.out.println(((Test)this).x);
}
}
class Test extends Test1 {
public int x = 2;
public static void main(String[] args) {
new Test().x(); // line 1
new Test1().x(); // line 2
}
}
Line 2 would give an exception.
Regards,
Lalitha
Muzammil Khan
Greenhorn

Joined: Jul 22, 2003
Posts: 8
Hi Jose Botella,
Thankyou very much for the welcoming me.
I have a question for you regarding the solution you suggested.
((Test)this).x
How can this be referenced from a static context?
Muzammil Khan
Greenhorn

Joined: Jul 22, 2003
Posts: 8
Hi Lalitha Chandran and Jose Botella,
Hats off to you two.
I think I have a lot to learn from javaranch.
Cheers
Muzz
MSC Computing
NOTHING WORTH WHILE WILL EVER BE ACHIEVED WITHOUT DEEP THOUGHT AND HARDWORK - J.R.D TATA
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
So I guess the final verdict is that java has polymorphism for non-static methods but not for member var. How would design an application where you need polymorphism for var as well?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
I will try to answer my own question:
So I guess the final verdict is that java has polymorphism for non-static methods but not for member var. How would design an application where you need polymorphism for var as well?

How about a minor change in my code at the beging of this post:

[ September 23, 2003: Message edited by: Barkat Mardhani ]
Nidhi Bangur
Greenhorn

Joined: Sep 12, 2003
Posts: 19
I am confused now. According to the code that Barkat posted, it means that if x is initialized in a variable initializer, the value in inherited method is picked up from superclass while if it is declared in instance initializer, its value is picked up from the subclass itself rather than superclass.
Why? What are the rules to know which value of the variable will be used in the inherited (but not overridden) method in a subclass?
Thanks,
Nidhi
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Nidhi

it means that if x is initialized in a variable initializer, the value in inherited method is picked up from superclass while if it is declared in instance initializer, its value is picked up from the subclass itself rather than superclass.

Well, Barkat's initial code was having two variable 'x'. One in Super class and one in Sub class BUT the latest code you saw from Barkat "removed" one of the 'x' variables. Barkat removed the "int x =2;" from Child class and instead put "initializer" block saying "{x=2;}", right? So, now there is only ONE copy of 'x' that is in Super class. When we call Test().x() here is the sequence of operations happened,
1. Called Test constructor
2. Called Super class's constructor (Test1())
3. Instance variables of Test1 got initialized where x got value=1
4. Control returned back to Test constructor
5. Instance variables of Test got initialized (there is no variable in Test now as we remove "int x=2;" from there
6. Instance block initializer got called for Test which is {x=2;}
(Which 'x' does this refer to??? Test1's 'x' because there is only one copy of 'x' now that is in Test1)
7. method x() got called on Test object that got created which prints 'x' which is 2 now.
Nidhi, please dont get confused with different approaches we saw here for getting '2' as the output that are,
1. Overriding method x()
2. Doing casting like Jose suggested ((Test)this).x
3. Last approach by Barkat where we removed one copy of 'x' from Child class
I hope it explains it...
Regards
Maulin
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Thanks Maulin. That was great explaination. Barkat
Nidhi Bangur
Greenhorn

Joined: Sep 12, 2003
Posts: 19
I agree with Barkat. Thanks Mauline, it was really well explained. I missed the point where Barkat had removed the 'int i' declaration from subclass.
-Nidhi.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hidding of member var