File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Dynamic Dispatching 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 » Java » Beginning Java
Bookmark "Dynamic Dispatching" Watch "Dynamic Dispatching" New topic
Author

Dynamic Dispatching

Meghana Ghanekar
Greenhorn

Joined: Oct 24, 2005
Posts: 6
Hi!
It is said that by default all methods of a class are virtual & the calls to the methods that are not private static or final are resolved at runtime.but in my Code this condition creates som problem.Please refer the code below:

class Base
{
Base()
{
System.out.println("in Base constructor");
draw();
}
void draw()
{
System.out.println("in base draw");
show(); //want to call the show of base
}

void show()
{
System.out.println("in base show ");
}
}
class Derived extends Base
{
Derived()
{
System.out.println("in derived constructor");
}
void draw()
{
System.out.println("in derived draw");
super.draw();
}
void show()
{
System.out.println("In derived show");
}
}

public class DynamicDemo{
public static void main(String[] args)
{
Derived d = new Derived();
d.draw();
}
}

I expected the output as:
// o/p for the constructor calls while creating object
In Base Constructor.
In Base Draw
in Base show
In derived constructor
// o/p for derived's draw
In derived draw
In Base draw
In Base show

But the output that i have got is as follows
//o/p for the constructor calls while creating object
in Base constructor
in derived draw
in base draw
In derived show
in constructor of derived
//o/p for derived's draw
in derived draw
in base draw
In derived show

I did understand the output it is like this because draw & show are virtual methods of Base & are overridden by Derived. Since the object is of type derived all the calls are resolved dynamically & thus are for Derived class.
But logically when i call any method of base in another method i expect it to be resolved for Base only & not for Derived. But it does not work this way. Is it an Flaw in the Language or it has got any special significance. Or is their any other reason.

Warm Regards,
Meghana.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Please paste your code in the code tags so it's easy to read and respond to.


ASCII silly question, Get a silly ANSI.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3700
    
    5

Although you have a pointer to a parent class, you don't neccessary have an instance of that parent class, so there's no guarentee that when you invoke a non-final method on the class, that the specific parent version of the method will be invoked (even when you invoke it from the parents constructor).

The reason for this relates to good encapsulation, a class is really a black box and via inheritance you may be holding an instance of the class or some child.

A good example of when this is useful is a Factory pattern related to databases. You request an instance of a Data Access Object (DAO for short), the system reads the current database settings and grants you a specific instance such as OracleDAO or SQLServerDAO, of which is returned by the factory as some GenericDAO. This allows programmers to override certain methods for specific databases (or for example, to have everything transmitted via file and throw out the database all together) with pure transparency to the user. The user doesn't know which database DAO they have, but they don't need it.

If you are coding the parent class and absolutely need a specific version of a method, you should declare it final.


My Blog: Down Home Country Coding with Scott Selikoff
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Simply a draw() would mean this.draw().
So at the Base' class if you say draw() or show() JVM takes it as this.draw() and this here refers to the current object being constructed, which is the derived Classes's object.

To confuse yourself more try the methods with static.
[ November 13, 2005: Message edited by: Arun Kumarr ]

If you are not laughing at yourself, then you just didn't get the joke.
Meghana Ghanekar
Greenhorn

Joined: Oct 24, 2005
Posts: 6
Hi Arun!
If the refernce is for current object this means that i can access the things that are not declared in base but are in derived. but this is not possible this still is a base refernce the call to method is a call to virtual method thus it is resolved at runtime. this creates a problem.
It is also said that the call is resolved using method table . Can anyone suggest me some book to refer where i can get more information about this method tables.

Thanx,
Meghana.
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

In derived classes' constructor the first method call would be a call to the super();
Even if we don't explicitly insert one the JVM inserts a super(); and calls the super classes' constructor.

Calls from a constructor/method is resolved at runtime. The JVM sees if the call for a method in the constructor/method is invoked in the current class or a sub-class.
If it is from a sub-class, it checks if the sub-class overrides the method of the base class.
a) If it does, it calls the sub-classes' method.
b) If it doesn't the super classses' method is called.

Runtime polymorphism.
[ November 14, 2005: Message edited by: Arun Kumarr ]
Raghuveer Kumarakrishnan
Ranch Hand

Joined: Mar 13, 2005
Posts: 32
If the refernce is for current object this means that i can access the things that are not declared in base but are in derived. but this is not possible this still is a base refernce the call to method is a call to virtual method thus it is resolved at runtime.


Hi Meghana you are getting confused here.

The reference variable is used to check what method can be run BUT what version of the method runs depends on the object.

Let me explain with this example.



Hope this helps


Raghu<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

This is not a flaw in the design of the language -- this is, in fact, what is desired. When you call a method, you should be concerned solely with the fact that the method is called -- not how it accomplishes that. Take, for example, the following:



In the reproduce method of Mammal, you are calling the giveBirth method. But if your current object is a Monotreme (a Platypus or an Echidna), you do not want the Mammal's version of the method to be called, because Monotremes cannot give birth to live young. They just can't. (Note the Unsupported operation exception if you try to call it manually...) So even though the method is called in the super class, the method is resolved at runtime to call the correct version of the method.

Now, if you say that this is causing a problem, the it is likely that you need to re-engineer some part of your code. Some is-a logic just isn't being handled correctly. If you can point to exactly why this is a problem and what you are trying to do, we can help you with that.

(And there is a way around this using Reflection, but I won't mention how to do it unless we've exhauseted other possibilities; simply put, you should not need to get around this....)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
You have exposed an unintuitive, and unnecessary, behaviour as a result of "calling an overrideable method on 'this' from a constructor'. This is why you should never do it.

Here is some more information: http://jqa.tmorris.net/GetQAndA.action?qids=10&showAnswers=true


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic Dispatching
 
Similar Threads
A simple Q but still have doubt
Static methods & Overriding!
Question about Parent call childe method
few basic questions....
Pls help me out..