• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hide and inherit

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, dennis.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"(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
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Dennis, I was referring to Smalltalk.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"(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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic