Java: polymorphic summary

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43336822/article/details/101713924

Polymorphic review

First, we must first clearly define polymorphic: refers to allow objects of different classes respond to the same message. That is the same message can be sent, depending on the object while using a variety of different behavior , the news is called method.

Polymorphism is one of the four characteristics of java, its implementation principle (generally dynamic polymorphism) is an address covered , it follows [ type parent class object reference when the variable reference subclass object, the referenced object is not referenced type variable determines who to call a member method (provided that this method is been defined in the parent class, that is has been overridden by subclasses method)] principle.

Table method is the core dynamic invocation, there is a method table entry method inherited from the parent class also has its own rewriting (override) method of entry, and each entry is a pointer. The reason why the reference point to the parent class subclass object is one of the conditions polymorphic, that is because the method table only subclasses of the parent class method subclasses rewritten method itself, only the concept of polymorphism.

Conditions java implementation polymorphism inheritance, rewriting and up modeling.

Polymorphic classification

(1) Static polymorphism (compile-time polymorphism, static binding)
method overload: ① the same scope (class); ② the same method name, parameter list of the different

(2) dynamic polymorphism (polymorphism of operation, dynamic binding)
conditions:
① inheritance;
② override method; (subclass covers override parent class)
reference ③ upward shape (base class: To subclass with no parent there, it references the parent class sub-class object, which is the parent class reference point to a subclass object.) Here Insert Picture Description
ha ha ha ha ha this image very image of friends ~ ~

Polymorphic advantages

1) Alternatively: the alternative of having a polymorphism of the existing code. For example, polymorphisms of the Circle class work circle, any other circular geometry, such as rings, also work.

2) Scalability: polymorphic code having scalability. Add a new sub-class does not affect the existing class of polymorphism, inheritance, and other operational and operational characteristics.

3) Flexibility: it reflects the flexible operation in applications to improve the efficiency.

4) simplicity: polymorphic simplify coding and application software modification process, especially in the processing of arithmetic operations and a large number of objects, and especially important to this feature.

Guess you like

Origin blog.csdn.net/qq_43336822/article/details/101713924