开发者

Java Base b = new Derived(); inheritance questions

What开发者_JS百科 exactly happens when you create a new instance using :

Base b = new Derived();

I cannot really understand the mechanics behind this.


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


Basically, from that point the compiler sees a Baseinstance and in runtime the instance is of type Derived.

In a broader explanation the Basetype 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜