Two classes and one override
This system is a Java program: each platform is a class. Car extends Vehicle and overrides start(): same signature, different code.
This system is a Java program: each platform is a class.
Car extends Vehicle and overrides start(): same signature, different code.
This system is a Java program: each platform is a class. Car extends Vehicle and overrides start(): same signature, different code.
The light blue particle is the execution flow. In main, it creates a Car object.
It stores it in a variable of type Vehicle: a Car is a Vehicle.
Declared type: Vehicle. Real type of the object: Car.
The light blue particle is the execution flow. In main, it creates a Car object. It stores it in a variable of type Vehicle: a Car is a Vehicle. Declared type: Vehicle. Real type of the object: Car.
Before running, the compiler checked the call by looking at the type of the variable.
Vehicle declares start(). The variable defines what can be called.
Before running, the compiler checked the call by looking at the type of the variable. Vehicle declares start(). The variable defines what can be called.
Which code runs? The variable says Vehicle: it would seem to be its version.
But the JVM does not look at the variable: it follows the reference to the object.
Which code runs? The variable says Vehicle: it would seem to be its version. But the JVM does not look at the variable: it follows the reference to the object.
The object is a Car: the JVM picks its start(). That is dynamic dispatch.
Polymorphism: the variable says Vehicle, but the code of Car runs.
The object is a Car: the JVM picks its start(). That is dynamic dispatch. Polymorphism: the variable says Vehicle, but the code of Car runs.
Inside Car, super.start() calls the version of the parent.
First, what every vehicle does runs; then the flow returns to Car.
Car adds its own part: it extends the behavior instead of replacing it.
Inside Car, super.start() calls the version of the parent. First, what every vehicle does runs; then the flow returns to Car. Car adds its own part: it extends the behavior instead of replacing it.
Overloading is something else: several methods with the same name in one class.
The compiler picks one by looking at the argument types: 3 is an int.
Everything is resolved before running: the flow goes straight to print(int).
Overloading is something else: several methods with the same name in one class. The compiler picks one by looking at the argument types: 3 is an int. Everything is resolved before running: the flow goes straight to print(int).
Overloading: the compiler decides. Overriding: the JVM decides, on every call.
The variable defines what can be called; the object defines what runs.
That is how you write code that works with subclasses that do not exist yet.
Overloading: the compiler decides. Overriding: the JVM decides, on every call. The variable defines what can be called; the object defines what runs. That is how you write code that works with subclasses that do not exist yet.