wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Dynamic method dispatching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dynamic method dispatching" Watch "Dynamic method dispatching" New topic
Author

Dynamic method dispatching

subash chandar
Greenhorn

Joined: Feb 07, 2005
Posts: 8
What is dynamic method dispatching and what are the advantages and disadvantages of using this concept
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm not sure what was meant by that. Where did you run into the term?

It might have been talking about polymorphism. Different classes can have different behavior for the same method. When you refer to an object by an abstract type and invoke methods based on the abstract type, the JVM dynamically figures out exactly what code to run.

Say you have a method that takes a Map argument and you call the put method. At compile time the compiler doesn't know what kind of Map you'll get, so it can't compile a call to a particular put method. At runtime somebody passes you a HashMap or a TreeMap or a CustomMap and the JVM dynamically looks up the put method and invokes it.


There are a couple other things "dynamic" might have meant, like reflection or some design techniques. Let me know if polymorphism seems to fit your context.
[ February 07, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
subash chandar
Greenhorn

Joined: Feb 07, 2005
Posts: 8
I try the follwing code

class a {
int i=10;
public void test() {
System.out.println(i);
}
}
class b extends a{
int i=20;
public void test() {
System.out.println(i);
}

public static void main(String args[]) {
b bobject=new b();
b=new a();
System.out.println(bobject.i);
bobject.test();
}
}


The output of the above method is 10 and 20

The stmt "System.out.println(bobject.i)" prints the value of i
which is declared in class a but the stmt "bobject.test()"
prints the value of i which is declared in class b.
I dont know whats the concept behind that
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
b=new a();

Does your code compile?


Mike Gershman
SCJP 1.4, SCWCD in process
subash chandar
Greenhorn

Joined: Feb 07, 2005
Posts: 8
I typed the wrong stmt.

It is not
public static void main(String args[]) {
b bobject=new b();
b=new a();
System.out.println(bobject.i);
bobject.test();
}

The correct stmt is

public static void main(String args[]) {
a aobject=new a();
a=new b();
System.out.println(aobject.i);
aobject.test();
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

But this still won't compile. There's no variable named "a" declared anywhere.


[Jess in Action][AskingGoodQuestions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4446
    
    5

Ignoring the syntax errors, it looks like "dynamic dispatching" in this case is what Stan explained above regarding polymorphic method calls. In other words, the actual method that will be called is resolved at runtime. In contrast, "static" dispatching is when the actual method to call is known at compile time. The difference between the two boils down to the way the locations of the methods to call are calculated.


Junilu - [How to Ask Questions] [How to Answer Questions]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Dynamic dispatching is a computer science term for what Java calls runtime polymorphism, calling instance methods based on the class of the object on which the method was called, as opposed to calling static methods based on the type of the reference variable on which the method was called.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic method dispatching
 
Similar Threads
Dynamic method lookup
overriden method
this operator
Dynamic Mehod Dispatching In Memory?
RequestDispatcher!