Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Dynamic method dispatching

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

Does your code compile?
 
subash chandar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But this still won't compile. There's no variable named "a" declared anywhere.
 
Sheriff
Posts: 16718
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic