开发者

What is dynamic method dispatch and how does it relate to inheritance? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
开发者_高级运维

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 6 years ago.

Improve this question

What is meant by dynamic dispatch in Java, and why do we need it in the context of inheritance?


What is meant by dynamic dispatch in Java […]?

Think of "dispatch" as "determining which method to call".

The "dynamic" part simply says that it is determined at runtime. That is, which method is to be called is determined at runtime.

why do we need it in the context of inheritance

Without inheritance / polymorphism we wouldn't need this. The type of an expression would be decidable at compile time, and which method that would have been called at runtime would be known when compiling the program.

With inheritance / polymorphism we don't know the concrete runtime type of an expression, thus which method to be called must be "dynamically" determined during runtime.

Without dynamic dispatch, virtual methods wouldn't make sense, which is central for abstraction and encapsulation.

Recommended reading: Wikipedia article on Dynamic Dispatch


Other Answers discus the theory, this is an example to show why dynamic dispatch (also called late binding) is needed.

Assume you have an Class 'Rectangle`.

public class Rectangle{
  public void draw() {
     System.out.println("___\n| |\n---");
     //does it look nice?
  }
}

And you have a subclass with rounded corners

public class RoundedRectangle extends Rectangle {
  @Override
  public void draw() {
     System.out.println("assume it is a rectangle with rounded corners");
  }
}

Now assume you have a method getting with an Parameter of Type Rectangle, which is used in the method to invoke the draw method

public class Demo() {
...
  public demonstration(Rectangle rect) {
    rect.draw();
  }
...
}

It the argument of this method is of class Rectangle then it will draw

___
| |
---

But when the argument is of type RoundedRectangle, then we expect:

assume it is a rectangle with rounded corners

And this is where late binding is needed for: When the code compiles, it is not clear which method needs to be invoked by rect.draw(); I could be Rectangle.draw(), RoundedRectangle.draw(), or the draw method of any other yet not implemented subclass of Rectangle.

Summary (from the view of an developer):

So late binding means: invoke the method of given signature (Method name, and argument list) for an instance where is only known that it is of an specific class or subclass - but it is not known which class it is exactly. And when a method is overriden, then ivoke the overriding method.

So there is no Polymorphism vs. Late Binding -- It is you need Late Binding when you have Polymorphism. In Java and in C++ (and i belive, in every other Object Oriented language too)


What is dynamic method dispatch in case of java

Same as it is in any other language. Despatching dynamically to a method chosen on the basis of the type of the object the method is invoked on.

and why do we need that in case of inheritance(what is the need of DMD)

That's the only time you do need it. If you don't have inheritance there is nothing to be dynamic about.

Which one is better Polymorphism in C++ or DMD in java.

The question is meaningless, as they aren't different. DMD is a mechanism for implementing one aspect of polymorphism.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜