Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphism doesn't work in method arguments in Java

I wrote the following piece of code:

class Plane {}
class Airbus extends Plane {}

public class Main {

    void fly(Plane p) {
        System.out.println("I'm in a plane");
    }

    void fly(Airbus a) {
        System.out.println("I'm in the best Airbus!");
    }

    public static void main(String[] args) {

        Main m = new Main();

        Plane plane = new Plane();
        m.fly(plane);

        Airbus airbus = new Airbus();
        m.fly(airbus);

        Plane planeAirbus = new Airbus();
        m.fly(planeAirbus);

    }
}

And the result is:

I'm in a plane
I'm in the best Airbus!
I'm in a plane

Unsurprisingly the two first invocations give I'm in a plane and I'm in the best Airbus! respectively.

Plane planeAirbus = new Airbus();

The method treats this object as a Plane, even though the real object is an Airbus. Even when I add abstract to class Plane, nothing changes and the result of last invocation is still I'm in a plane

So the question is why polymorphism doesn't work in method arguments and invocations? Is there any purpose of that? How does it work?

like image 541
Bartłomiej Szałach Avatar asked Apr 19 '16 14:04

Bartłomiej Szałach


1 Answers

The problem here is that Java does not support dynamic binding of method arguments. What you see is static binding, i.e. the overload of the method to call is chosen at compile time.

See also: Static Binding and Dynamic Binding

like image 131
Martin Klinke Avatar answered Oct 17 '22 15:10

Martin Klinke