Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hide and inherit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hide and inherit" Watch "hide and inherit" New topic
Author

hide and inherit

Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57

In the above program, class Q's instance variable b hide class P's b and class Q inherits m() method from class P, how to explain that when object q calls m() method, it doesn't "see" its own b but class P's b?
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Methods access variables only in context of the class of the object they belong to. If a sub-class method calls explicitly a super class method, the super class method always will access the super-class variable. Super class methods will not access the shadowing variables declared in subclasses because they don't know about them (When an object is created, instances of all its super-classes are also created).


SCJP 1.4<br />SCWCD 1.4
Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
Thanks a lot, dennis.
Claudio Roitman
Greenhorn

Joined: Oct 31, 2003
Posts: 18
"(When an object is created, instances of all its super-classes are also created)."
Really? Is that a good Idea? I don't think so. I preffer the "Smalltalk way", when You create an instance, You just create it, not superclass instances, and when You call a method defined in a superclass, your instance executes the code from superclass definition, but in its own context...
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hello Claudio. Sorry I dont know much about Smalltalk so I cannot comment 'bout it.
when You create an instance, You just create it, not superclass instances, and when You call a method defined in a superclass, your instance executes the code from superclass definition, but in its own context

This is interesting. Are you referring specifically to Smalltalk?
Because I dont think this is how Java operates. In Java, you can't make a new object without invoking not just the constructor of the object's actual class type, but also the constructor of each of its superclasses. In fact, the last constructor in an inheritance hierarchy is java.lang.Object. Its not that I choose to do that but that is how Java works. It does make a lot of sense though because if you are reusing inherited methods from a parent class you would want that method to be able to use its own set of fields. But if you rather have the method use your own fields then what you could do is override inherited methods.
Thanks.
Claudio Roitman
Greenhorn

Joined: Oct 31, 2003
Posts: 18
Yes Dennis, I was referring to Smalltalk.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
"(When an object is created, instances of all its super-classes are also created)." Really?

Claudio, you might be interested in how the Java Language Specification describes memory layout when an instance is created JLS 12.5
Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden (�8.3). If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (�4.5.5).

From this description, I have always visualized one instance holding the variables of the subclass and all its superclasses.
And here is what The Java Virtual Machine Specification says about the new instruction JVMS new
Memory for a new instance of that class is allocated from the garbage-collected heap, and the instance variables of the new object are initialized to their default initial values

----
Also, I checked Timothy Budd's An Introduction to Object-Oriented Programming, because he describes the differences between statically typed languages, such as C++, Eiffel, Java and dynamically typed languages such as Smalltalk and Python.
When he describes Memory Layout in Chapter 12, he does not make a distinction between Java and Smalltalk. Instead, he describes three approaches, and he groups Object Pascal, Smalltalk, Java and Objective-C together using the third approach. The default behavior of C++ is different.
[ November 26, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
In the above program, class Q's instance variable b hide class P's b and class Q inherits m() method from class P, how to explain that when object q calls m() method, it doesn't "see" its own b but class P's b?

The expression name b in the statement System.out.print(b); is resolved at compile-time. First, the compiler checks for local declarations of b. Then the compiler looks for declarations in the class or inherited from the direct superclass or direct superinterfaces.
At run-time, when a reference to an instance of the subclass is known, the virtual machine does not alter the choice of variable declaration made by the compiler.
----
Claudio, you might ask, Why is it that way? Why is the name bound at compile-time? The JLS and The Java Programming Language do not say why. So we look for clues. Timothy Budd, An Introduction to OO Programming points out the trade off between compile-time efficiency of statically typed languages (Java) and the run-time flexibility of dynamically typed languages (Smalltalk).
[ November 26, 2003: Message edited by: Marlene Miller ]
Claudio Roitman
Greenhorn

Joined: Oct 31, 2003
Posts: 18
Originally posted by Claire Yang:

In the above program, class Q's instance variable b hide class P's b and class Q inherits m() method from class P, how to explain that when object q calls m() method, it doesn't "see" its own b but class P's b?

...But if We have:
class P {
int b() {
return 3;
}
//instead of int b=3
void m() {
System.out.print(b());//instead of print(b)
}
}
class Q extends P {
int b() {
return 5;
}
//instead of int b=5
public static void main(String[] args) {
Q q=new Q();
q.m();
System.out.println(", "+q.b());
//instead of System.out.printl(", "+q.b); }
}
The output is 5,5 !!!
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Claudio. Are you asking a question? Would you like an explanation? Or did you want to show us your example for our enlightenment?
[ November 27, 2003: Message edited by: Marlene Miller ]
Claudio Roitman
Greenhorn

Joined: Oct 31, 2003
Posts: 18
I just wanted to show the difference.... I guess the explanation is:
Methods access variables only in context of the class of the object they belong to
.
It says: "access variables" but it is different for accessing methods.
But the afirmation:
(When an object is created, instances of all its super-classes are also created).
suggest the instance that actually executes m(), is an Instance of P (The superclass) but if it were an instance of P it would call P implementation of b() instead of Q implementation...
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hello Claudio.

Brief overview of above code:
First, m() method from class P is inherited. Inheriting the method is almost like the method really exist in its own class even though its not.
Second, b() from class P was overridden in class Q. Overriding means you are redefining the way the original method behaves.
Explanation:
In line 9, class Q calls m() which it has inherited from class P. A call to b() within method m() is made in Line 20, but the question is which b() method? Java sees that the method was redefined in Q so it calls that instead of the one in class P. This is called virtual method invocation where Java dynamically decides at runtime which method to call.
Difference between your code and Claire's code above? Very Big! Claire's code uses b as a variable, whereas, in your code b is a method. When that happens, its a different story. Variables are never overridden and as Marlene pointed out, variables are resolved at compile time. In other words, Java does not decide at runtime which variable to use unlike its behavior when dealing with methods. Variables however can be hidden and in the case of Claire's code, they are hidden within class Q so that any code running -->"within class Q"<-- and using the variable will not use the one defined in the super class P. Hence the word, "hidden". Claire's method m() is running -->"within class P"<-- and when it uses variable b it looks for it within that class.
Remember, your code does overriding which is applicable to instance methods only and not variables, static methods, and final methods.
I hope i explained it well. Other gurus out there please correct me if i'm wrong.
thanks.
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Claudio, in support of my earlier post i've modified your code. I changed b() method to static to show virtual/dynamic method invocation does not work for variables, statics, and final methods. Run the code and see why the difference in result.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hide and inherit
 
Similar Threads
overloaded methods taking object arguments
anotherone from Dan's
a b c
equals and to string method
what does this mean?