Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic method dispatching

 
subash chandar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is dynamic method dispatching and what are the advantages and disadvantages of using this concept
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
subash chandar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b=new a();

Does your code compile?
 
subash chandar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this still won't compile. There's no variable named "a" declared anywhere.
 
Junilu Lacar
Bartender
Posts: 7578
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic