You are "hiding" the name field of the super class. The code in the Father class has to refer to its public field called name. But, when you declare a field in the subclass, Son in this case, with the same name as the field in the superclass, you "hide" the superclass' field. This should really bake your noodle. Try this...
See what that prints.
James Carman, President<br />Carman Consulting, Inc.
With other words, fields aren't polymorphic, only (non-static) methods are.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Mar 04, 2005
Thnx for your comments, guys. I have read each post very carefully but still cant figure out the answer of my question. My questions is:
When you invoke son1.getName(), the java interpreter invoke the getName method defined in the superclass Father with an implicit parameter "this" which refer to son1, one object of Son. Since "this" refers to son1, "thie.name" should refer to the "name" field of son1, whose value should be son1.
I have used C++ for a long time. I think the this keyword in Java is similar to a object pointer in C++. In C++, if the pointer points to one object, let's say son1, the value of this->name will absolutely be "son1", not "father". Maybe Java is a little bit different from C++ on this issue. Can anyone tell me the mechanism behind this? Thnx a lot [ May 07, 2005: Message edited by: Jon Lee ]
Joined: Mar 04, 2005
I have figured it out, the following is my thought:
Rule No1: subclass inherits all the methods and fields in its superclass. But subclass can not access the private methods and fields of the superclass.
Rule No2: "When you create an object of the derived class, it contains within it a subobject of the base class. This subobject is the same as if you had created an object of the base class by itself. It�s just that from the outside, the subobject of the base class is wrapped within the derived-class object." -- From <<Thinking in Java>>
Based on the above two rules, I have the answers to my question: Son class has two "name" field. One belongs to Son class. The other is inherited from the Father class and it is hidden by the "name" field of Son Class.
Object of Son class has an subobject of Class Father. The subobject contains a "name" field whose value is "father" and it is hidden from the outside. When invoke son1.getName(), son1 is upcasted to the type Father because class Son does not override this method. After the upcasting, interpreter regards son1 as a reference to that subobject which contains a name field with value "father". That's why you can "father" in the second line of output.
The value of "this" is a reference to the object, son1 in this case, which originally invoke the method.
Joined: Jul 11, 2001
Originally posted by Jon Lee: When invoke son1.getName(), son1 is upcasted to the type Father because class Son does not override this method. After the upcasting, interpreter regards son1 as a reference to that subobject which contains a name field with value "father". That's why you can "father" in the second line of output.
What you need to understand here is the difference between late binding and early binding.
Binding is the mechanism of how the system decides what actual entity is meant by using a symbolic name.
Java uses late binding for instance methods, that is, the compiler just stores the symbolic name in the byte code, and the VM decides at runtime which method to execute. That allows it to base the decision on runtime information such as the actual type of an object - which results in polymorphic behaviour.
For fields, though, (as well as for static methods), Java uses early binding. That is, the symbolic name is resolved to the actual entity at *compile* time. Now, when the compiler compiles the Father class, it doesn't know about a field of the same name in a subclass - the only fields it can access are those accessible from Father. So it's hardcoded into the byte code that Father.getName will always access Father.name, no matter what type the actual object (this) is of.
Does that help?
Joined: Mar 04, 2005
Yes mate. Your post make a lot sense to me. Thnx so much. I confuse "this" in java with the pointer in C++....