Warum Polymorphismus verwenden?

Ich habe den folgenden Code, in dem ich eine Elternklasse und ihr Kind habe. Ich versuche festzustellen, wie der Code von der Verwendung des Polymorphismus profitiert.

class FlyingMachines {
    public void fly() {
        System.out.println("No implementation");
    }
}

class Jet extends FlyingMachines {
    public void fly() {
        System.out.println("Start, Taxi, Fly");
    }

    public void bombardment() {
        System.out.println("Throw Missile");
    }
}

public class PolymorphicTest {
    public static void main(String[] args) {
        FlyingMachines flm = new Jet();
        flm.fly();

        Jet j = new Jet();
        j.bombardment();
        j.fly();
    }
}

Was ist der Vorteil von Polymorphismus, wenn beideflm.fly() undj.fly() gib mir die gleiche antwort

Antworten auf die Frage(13)

Ihre Antwort auf die Frage