What exactly happens when you create a new instance using :
Base b = new Derived();
I cannot really understand the mechanics behind this.
Basically, from that point the compiler sees a Base
instance and in runtime the instance is of type Derived
.
In a broader explanation the Base
type might be a interface, so you know by the contract what method has and what it does Polymorphism
. But you are abstracted from the implementation.
The reference to b
is type Base
. But the implementation is Derived
. This means you can use it as a Base
but it will behave as a Derived
. Doing b instanceof Derived
will be true because the implementation is of type Derived
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.