TIJ swot_JAVA的初学者(四)

1.4 Reusing the implementation

We could reuse a class by instantiate it as an obect or place an object of this class inside a new class(this is called composition or aggregation).

1.5 Inheritance: reusing the interface

Inheritance enables us to clone an existing class and make additions and modifications to the clone.The modified “clone” will reflects the change in the original class.Do we need to modify the clone class?

Inheritance represents a certain kind of relationship between classes/types using the concept of base types and derived types. A base type contains all of the characteristics and behaviors that are shared among the types derived from it.

By inheritance, we could  add new function to the interface or change the behavior of an existing base-class function.This referred to as overriding that function.

1.5.1 Is-a vs. is-like-a  relationship

If inheritance override only base-class function, this is a pure substitution.Like “a circle is a shape“.

When we add new interface elements to a derived type, this substitution isn't perfect.This can be described as an is-like-a relationshipe. Such as “heap pump is like a cooling system".

1.6 Interchangeable objects with polymorphism

By inheritance, we could create a lot more new objects, which has similarities as the base objects. But is that all?

Let me think...We got too many methods or functions have the same name or identification as a result of inheritance. Polymorphism is the mechanism of OOP to pick up the right one automatically. But we dont need to know how it does this at this stage.

In a non-OOP compiler, the function call generated is early binding, that means the absolute address is needed. In OOP, the program cannot determine the affress of the code until run-time. This concept is called late binging. To perform this, Java uses a special bit of code in lieu of the absolute call. This code calculates the address of the function body, using information stored in the object.This is Java's dynamic binding.

upcast..

1.6.2 Abstract base classes and interfaces

To declare an class abstract, we prevent creating an object of that class.

We can declare a class or a method as abstract, but abstract method can only be created inside an abstract class.

To take the concept of abstract class one step further, we could declare a class as an interface. Any implementation of methods is strictly forbidden in an interface.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值