Java Object-oriented---polymorphism

Source: Internet
Author: User

Polymorphism refers to allowing objects of different classes to respond to the same message. Polymorphism consists of parameterized polymorphism and inclusion polymorphism. Polymorphism language has the advantage of flexibility, abstraction, behavior sharing and code sharing, which solves the problem of application function with the same name.
There are two forms of polymorphism: overloading and covering
The first thing to say about overloading (overload) is that it happens in the same class. It has nothing to do with what parent subclass, inheritance.
Identifies a function in addition to the function name, as well as the parameters (number and type) of the function. In other words, a class can have two or more functions, called the same name, and their arguments are different.
They have nothing to do with, they are different functions, just maybe they function similarly, so just name it, increase readability, that's all!

Again overwrite (override), is happening in the subclass! In other words, there must be inheritance in case the overwrite occurs.

We know that inheriting a class, there is a parent class of all methods, if you feel that the method is not comfortable, the function to change, then the function in the subclass to re-implement it again.
When this method is called again, the procedure in the subclass is executed. The functions in the parent class are overwritten. (Of course, the function name and argument are exactly the same as in the parent class, otherwise your method has no effect on the methods in the parent class, because they are two functions, not related)

Benefits of polymorphism:

1. Replaceable (substitutability). Polymorphism is replaceable for existing code. For example, polymorphism works on the Circle Circle class and works on any other circular geometry, such as a ring.
2. expandability (Extensibility). Polymorphism has extensibility for code. Adding new subclasses does not affect the polymorphism, inheritance, and the operation and manipulation of other attributes of existing classes. In fact, new subclasses are more likely to get polymorphic functions. For example, it is easy to add the polymorphism of sphere class on the basis of realizing the multi-state of cone, semi-cone and hemispherical body.
3. Interface (interface-ability). Polymorphism is a superclass that, by way of signature, provides a common interface to subclasses, which is implemented by subclasses to refine or overwrite the class. As shown in 8.3. The super-Class shape in the figure specifies two interface methods for implementing polymorphism, Computearea () and Computevolume (). Subclasses, such as circle and sphere, refine or overwrite both interface methods in order to achieve polymorphism.
4. Flexibility (flexibility). It embodies the flexible operation in the application, and improves the use efficiency.
5. Simplification (simplicity). Polymorphism simplifies the process of coding and modifying the application software, especially when dealing with the operation and operation of a large number of objects.

Java Object-oriented---polymorphism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.