Inheritance and Implementation

Foreword

Think of inheritance and implementation, they will think of abstract classes and interfaces, this article on abstract classes and interfaces simply summarized as the entrance, while inheritance and implementation of the extended summary.


Ordinary class and abstract class

1. abstract meaning of existence

For such multiplexing method, i.e. to achieve polymorphism, (the same strain to inherit)

2. Distinction

Compared to the general category and more an abstract way (can not be static, final modification) can only be modified permissions (public or protected)

Unable new abstract class, subclass constructor can only provide call

3. Advantages

Peremptory norms can be written, a clear need to cover methods

4. Extension

Why is a single multi-inheritance realization?

for example:

A method has eat (), B and C inherits and overrides of the EAT A ()

If, at this time the multiple inheritance B and D C, will lead to () method with the same name eat

However, the implementation of the method is rewritten every time (to ensure that in the B, C level), even if the B, C interface has eat () method, only a rewrite

Inheritance usage

Inheritance can be used in the expansion of the original class, such as a table definition after late need to expand the table function, you can add a new field to use inheritance.


Abstract classes and interfaces

1. Interface meaning of existence

Enhanced spreading of the code, its essence to its dregs (patchwork)

2. Distinction

Then abstract abstract class

A (public static) and constants (public abstract) abstract method consisting

After JDK1.8 can define a default implementation

No constructor (the difference between classes and interfaces, and the difference between multiple single inheritance implementation)

3. Advantages

Precise extensions, will not put unnecessary methods or variables inherited

4. Extension

When a new function, you only need to add a new interface, this interface can be

Guess you like

Origin www.cnblogs.com/guaosky/p/12543743.html