W dynamic submission method and how does it relate to inheritance?

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

+5
source share
3 answers

What is meant by dynamic dispatching in Java [& hellip;]?

Think of β€œsending” as β€œdefining a call method.”

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

why do we need this in the context of inheritance

/ . , , , .

/ , , , "" .

, .

:

+12

, , , ( ).

, Rectangle .

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

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

, get Type Rectangle, draw

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

Rectangle,

___
| |
---

RoundedRectangle, :

assume it is a rectangle with rounded corners

: , , rect.draw(); Rectangle.draw(), RoundedRectangle.draw() , Rectangle.

( ):

: ( ) , , , , . , .

, . , , . Java ++ ( , - )

+10

java

, . , , .

( DMD)

, . , , .

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

The question is meaningless because they are not different. DMD is a mechanism for implementing one aspect of polymorphism.

+1
source

All Articles