aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Data Hiding 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 "Data Hiding" Watch "Data Hiding" New topic
Author

Data Hiding

Sarah Jorden
Greenhorn

Joined: Sep 26, 2007
Posts: 16
This Question has already been asked but I did not get satisfactory answer in this thread,

http://www.coderanch.com/t/265544/java-programmer-SCJP/certification/Inheritance

in the below code,



Here,
String variable s at Line 2 in class Child will hide the String variable s at Line 1 in class Mom, right?

Now class Child is not overriding method test() but still when we call it at Line 3 and Line 4 , shouldn't it print Child class variable s which is hiding Mom class variable s?

Why is it printing "Mom" and not "Child" ?

please correct me, I must be missing something.
[ October 03, 2007: Message edited by: Sarah Jorden ]
vijaya bharath
Ranch Hand

Joined: Jun 10, 2005
Posts: 66
Hi,

I think i can explain better.
Let me expalin like this
When you created object for mom it will have methods.
You hide test2 and String s in child through inheritance.
when you call test method through child object it calls the parent method only.parent test method doesn't come and sit in your child class.child class inherits that means it can call that method with out creating any object to that class.
Calling the inherited test method through child object is same as calling the original test method from parent object.

and in the case , it prints string s as child only as you hid the mom with child .


Regards,<br />Vijaya Bharath.<br />SCJP1.4 <br />SCWCD5.0
Sarah Jorden
Greenhorn

Joined: Sep 26, 2007
Posts: 16


Output :

Mom : Test Mom
Mom : Test Mom
Mom : Test Mom


Here, I'm asking about Line 3 and Line 4, why b.test() and mb.test() prints string "Mom" which is variable s in class Mom.

In my opinion these two lines print "Child" variable s in class Child which is hiding s variable of class Mom.
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424

when you call test method through child object it calls the parent method only

When a class inherites a member its as if it has declared that member, calling method test() through a Child reference is done from Child Not from Superclass. Try replacing this method with test() in Mom class.

Sarah Jorden
Greenhorn

Joined: Sep 26, 2007
Posts: 16
Hey Ahmed,

Thanks for that nice example.

Now, if the method test() is bening called from Child class then
why is it printing variable s = "Mom" of class Mom and not variable s = "Child" from class Child? even though class Child has its own variable s.

right now, I'm getting this output,

Mom : Test Mom calling from class: Mom
Mom : Test Mom calling from class: Child
Mom : Test Mom calling from class: Child


but why am I not getting this output? ,

Mom : Test Mom calling from class: Mom
Child : Test Mom calling from class: Child
Child : Test Mom calling from class: Child


please correct me, Where am I going wrong?
[ October 04, 2007: Message edited by: Sarah Jorden ]
Sarah Jorden
Greenhorn

Joined: Sep 26, 2007
Posts: 16
I even made a small change to method test() like this,



output is same as before,


Mom : Test Mom calling from class: Mom // Line 1
Mom : Test Mom calling from class: Child // Line 2
Mom : Test Mom calling from class: Child // Line 3


Shouldn't this.s refer to the currently executing object's variable s?
In first line, we are refering to Mom object. But in second and third line, we are refering to Child object.

I'm not getting it. What am I missing here?
Teo Framoe
Greenhorn

Joined: Jan 05, 2007
Posts: 13
Hi,
Could anyone please take a look at this. I really don't get it.
Regards,
Teo
Sarah Jorden
Greenhorn

Joined: Sep 26, 2007
Posts: 16
me too.....
Chen SanHau
Greenhorn

Joined: Feb 06, 2004
Posts: 27
I think it's because of the implementation of JVM. Maybe it's the only reason to explain that.
In other language, it gets different result.

Java




PHP
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy!

Sarah asked:

Shouldn't this.s refer to the currently executing object's variable s?
In first line, we are refering to Mom object. But in second and third line, we are refering to Child object.

If a class inherits a method, this does not mean it would not be like pasting in the same code. Rather it is like you use the code of the superclass with your subclass's object.

In order to avoid confusion with your Mom and Child class names, here's a short example with classes A and B1 / B2. Both B's extend A, one has the method overriden, the other has not:

The output is
B1@82ba41
A-variable

B2@130c19b
B2-variable


You see the difference if the method is overridden with pasted code (same code as in the superclass) and if the method is just inherited.

In B1, there is no override and the object uses the superclass's method. Because of this the method does not see that variable s has a new value in the B1 class. It prints "A-variable".
In B2, there is an override, so here the method of the subclass is called, and therefore "B2-variable" is printed.


this will always return a reference the currently executing object, but this does not mean it will invoke a Child-method in all cases, because not in all cases the method is overridden.


When accessing the variables directly, there is no polymorphism at work. In this case only the type of the variable counts, so if you have
Mom variable = new Child();
and you print variable.s, it sees only Mom's s.
With variables, there is no polymorphism, only with methods.


Generally it is normal that you get some knots in your brain when you study this...



Yours,
Bu.


all events occur in real time
Teo Framoe
Greenhorn

Joined: Jan 05, 2007
Posts: 13
Knots untied
Thanks a lot Burkhard!

Teo
Sarah Jorden
Greenhorn

Joined: Sep 26, 2007
Posts: 16
thanks a lot Burkhard.



I was waiting for a clear answer like this for last two days.
Yogesh Baraskar
Ranch Hand

Joined: Oct 07, 2007
Posts: 33
thanks a lot Burkhard..
Very well explained.

To make it a simple rule:
1. If method is overridden by a sub-class and subclass has the same variable name as declared in Super class, then any call (whether it is on subclass reference or super class reference ) to that method will display the value of variable declared in subclass.
2. If method is not overridden then in that case ,when you call a method of super class it will always take the variable declared in super class(reason super class method doesnt have any visibility to any methods or variables declared in child class).

Please correct me if my above statements are wrong.

Regards
Yogesh
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Originally posted by Yogesh Baraskar:

1. If method is overridden by a sub-class and subclass has the same variable name as declared in Super class, then any call (whether it is on subclass reference or super class reference ) to that method will display the value of variable declared in subclass.

No, in that case each method call will reference its own variable.
Matthias Wendel
Greenhorn

Joined: Sep 26, 2007
Posts: 10
If the explanation of Burkhard is adequate, then the lightball hints in netbeans indicating "Field hides another field" in lines 11 and 16 of Burkhard's code are rather misleading, or am I still missing something?
Yours,
Matthias
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data Hiding
 
Similar Threads
objects eligible for GC?
SCJP
who have appeared in SCJP
Inheritance
What's the result of this inner class code?