Java object-oriented programming polymorphism example

This paper describes the polymorphism of java object-oriented programming. Share with you for your reference, as follows:

Polymorphism: the ability to represent multiple forms (the same implementation interface uses different instances to perform different operations)

Realize the advantages of polymorphism: in order to facilitate unified call!

Three ways to achieve polymorphism!

1. Conversion from subclass to parent class:

Example:

Rules:

① Pointing the reference of a parent class to a subclass object is called upward transformation, which automatically performs type conversion

② At this time, the method called through the parent class reference variable is the method of the child class overriding or inheriting the parent class, not the method of the parent class

③ At this time, you cannot call methods specific to subclasses by referencing variables through parent classes!

2. Use the parent class as the method parameter to realize polymorphism

3. Use the parent class as the method return value to realize polymorphism

be careful:

① The existence of inheritance (inheritance is the basis of polymorphism, there is no polymorphism without inheritance)

② Subclasses override methods of the parent class

③ The parent class reference variable points to the child class object

For more Java related content, interested readers can view the special topics of this site: introduction and advanced tutorial of java object-oriented programming, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>