What is to be understood by runtime polymorphism

Tags: design pattern

Descriptive text

This section describes an overview of object-oriented with the following three points:
Object orientedThree propertiesThe six relationships between class and classas well asSeven principles of object-oriented

Three characteristics object-oriented


You do not need to know the specific implementation details of the object and you need to make the function of the object available through a shared method


Use an existing class as the base class (father), build a new class on this basis (Subclass),
Subclasses can reuse the functionality of the parent class and can also be extended, which improves code reuse.
In contrast to C ++, Java can also inherit multiple higher-level classes at the same time, only tree inheritance, such as
Animal -> person -> human or byInterface and internal classes Realize multiple inheritance!


The same method of a class instance has different appearances in different situations, in general there are two

Compile-time polymorphism(Overload) method Overloaded
Runtime polymorphism(Override) - Inheritance + method override + parent class reference to the subclass object

Regarding this runtime polymorphism, you may not understand it, for example:

At this point, the problem occurs when there are two lines of code:

Little brother A = new hand tea brother ()
A.do ()

ask: After calling "do", is "do something" or "give the boss tea" printed?

, Which type is behind new and which type is dynamic type?

The relationship between class and class

Storage formula:Chicken Wet Clothes Crew(Inheritance, realization, dependency, association, aggregation, composition)
The first two inheritances and implementations are basically known, while the last four are fairSemantic level The difference,
The degree of correlation between the two classestrust < connected < Polymerization < combination
Before that, I believe that many of my friends have never touched UML class diagrams and may not understand them. This is the class and interface

1. Inheritance / generalization(Generalization)

One is the parent class and the other is the child classSolid line of the hollow triangle arrow Means
From child class to parent class or from child interface to parent interface.

2. Realize(Realization)

A class implements the interface, rewrites related methods, and uses themHollow triangle arrow dotted line Means

3. Dependency(Dependency)

Give a chestnut

The big man was thirsty and wanted to drink tea. He handed the tea boy, and the tea boy was only used as a tool for passing tea.
There is no use for eggs, you can go after the tea brother is handed over (Does not contain references

Specific performanceLocal variableFunction parametersReturn value

UML diagram(useSolid dotted line with arrowMeans from the usage class to the dependent class):

4. Association(Association)

Give a chestnut

The big guy usually has a bodyguard and boy around him. Of course, that kind of boy has to be on call, so
Stay in contact (Keep reference), the relationship with the tall guy will be stronger than that of the tea guy,
A boss can have multiple bodyguards, and a bodyguard can have multiple bosses (25 boys).

Specific performanceMember variables
UML diagram(Solid arrowIndicates that the arrow is pointing to the associated class, and this can be bidirectional (one to many or many to many):

5. Aggregation(Aggregation)

The same as the club is throughMember variablesExpress, however, the association is on the same level and the aggregation is on
YesIn whole and in part Hierarchical like:Societies WithLittle brotherAnd even if there is no club
The kids can still go to other places to do things. use Hollow diamond with a solid arrow Said.


Similar to aggregation, except that the degree is stronger and more symbiotic and the combined class is responsible for the life cycle of the combined class, such as:
Societies WithgangsterIf there is no society, the big brother will not exist.
useSolid diamond and solid arrow indicate Said.

Finally, let's create a large UML diagram based on these relationships:

Seven basic principles of object-oriented

1. Principle of individual responsibility(Single Responsibility Principle)

Each class should focus on one thing.
That means: high cohesion, low coupling

2. The principle of opening and closing(Open Close Principle)

An object is open for expansion and closed for modification.
That is, the changes to the class are made by adding code, rather than changing the existing code

3.Liskov substitution principle(Liskov Substitution Principle)

Wherever the superordinate class appears, it can be replaced by its subclasses.

4.Principle of dependency inversion(Dependence Inversion Principle)

Rely on abstraction, not on concrete implementation.

5.Interface isolation principle(Interface Segregation Principle)

The smallest possible interface should be made available to the client instead of a large overall interface.

6.Dimit principle(Law Of Demeter)

An object should interact with other objects as little as possible in order to make the system function modules relatively independent.

7.Combination / aggregation reuse principle(Composite / Aggregate Reuse Principle)

Try using composition / aggregation instead of inheritance

Annotation text

This article only went through the related concepts, understanding, and understanding and did not include any specific ones
If you want to see more content, you can go to Artisan Ruoshui
Blog view from:

Object-oriented design patterns and concepts of basic functions
Seven basic principles of object-oriented design patterns

Or read the book "Analysis and Real Battle of Android Source Code Design Patterns" for related content.

Change protocol

  • 2017.1.23 Partial content change