Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dynamic lookup

 
Richard Wilson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//look at code below:
class A {
public int Avar;
public A() {
System.out.println("AAA");
doSomething();//upto dynamic lookup
}
private void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class B extends A {
public int Bvar = 2222;
public B() {
System.out.println("BBB");
doSomething();
System.out.println("Avar=" + Avar);
}
public void doSomething() {
System.out.println("Bvar=" + Bvar);
}
public static void main(String[] args) {
new B();
}
}
In constructing B's object,the constructor calling super() but why it execute B's doSomething?
Why dynamic binding happens since
1. A's constructor do not know about the method
which is defined as well in subclass B
2. B's object has not been produced yet
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't quite understand what you were asking.
One thing to point out is that since doSomething() is private in class A, it will not be inherited by the subclass B.
Likewise, B's doSomething() method is NOT an overriden version of the one in A; they are completely unrelated. Marking a member public prevents it from being inherited by any subclasses.
The constructor in A will always call A's doSomething() method, because it is local to the class, and since it's private, does not have to worry about dynamic calls. The only doSomething() method A can call is A's version.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to follow the flow of initialization carefully. In main, you create a new object of type B. That calls the constructor of class B. The constructor B implicitly calls the constructor of A first. In the constructor for A, we call doSomething(). This is equivalent to saying this.doSomething(). Now, I ask this, what type is "this"? It is of type B. Therefore, the compiler checks to see if class B has a doSomething() method. It does, indeed, so that method is called.
Calling overloaded methods in a parent class' contructor can lead instance variables not being initialized properly and is generally not done. It's important to realize, however, that, even though B is in the process of being initialized, the object already exists and methods can be called on it.
Another question you ask is, how can doSomething be called from A when A doesn't know about it? Take a look at this code:

I'm sure you can see exactly how this works but, notice that doIt2() is being called in Subclass from Superclass in doIt(). This dynamic lookup is no different than what is occuring in your example.
I hope that helps,
Corey
 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard,
I just looked up ur question. I Cud answer it partially. In fact JLS states the exact similar situation as yours. 15.12.4.8
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#239650
The explanation given there makes sense.
any method call is basically a short form of
this.method()
and 'this' in this case is the subclass object .
[ February 18, 2002: Message edited by: Jennifer Wallace ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, Rob was right. I didn't notice the private modifier in class A. Had that modifier been changed to protected or public, the above argument would have help true.
Sorry about that.
Corey
 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops ! Missed the "private" modifier!
[ February 18, 2002: Message edited by: Jennifer Wallace ]
 
Richard Wilson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Sorry to mistype.The access modifier is "public" not "private".
 
Peter Bugla
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Corey McGlone:
This dynamic lookup is no different than what is occuring in your example.

Apart from this sentence you're right.
The situation mentioned by this thread's first post is a bit different, because we're in the process of constructing the objects, i.e. they are not fully initialized yet.
If you take the program from the original post
(with private already replaced by public) you get the following result:

It's interesting that the 2nd line states Bvar is 0, isn't it.
It happens because the instance variables are not initialized until the superclass is constructed which does make sense, of course.
Ideas for extremely difficult test questions come to mind.
I just wanted to point out, that dangerous side effects may arise by calling subclass methods from superclass constructors (respectively overriding superclass methods that are called in the superclass's constructor).
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is true. When constructing a derived object the parent constructor does its work on the reference to the just newly created object.
Polymorphysm also works within constructors. There is a risk using polymorphic calls in constructors: if the overridding method uses variables defined in the derived class, they won't be initialized by the derived constructor when executing the base constructor. They will just hold the default values.
Thus the overridding method will obtain the default value for them.
Ooops... I didn't see your post Peter
[ February 19, 2002: Message edited by: Jose Botella ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Apart from this sentence you're right.

Pater,
How is the dynamic lookup in the original example any different from the dynamic lookup in the example I gave? Just because the object is in the process of being instantiated, the dynamic lookup rules haven't changed. Granted, the instance variables of the subclass haven't been initialized yet, so they may give different values than you might expect, but they're not being accessed any differently.
Just trying to figure out what's "different."
Corey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic