Java study Note 6-Java class inheritance and polymorphism (2)

Source: Internet
Author: User

1. Concept of Polymorphism

Polymorphism is a major feature of object-oriented programming. ReflectedProgramScalability also reflects the programCode. Simply put, an object can be operated in many ways based on different objects. Therefore, polymorphism is pinned on objects.

2. Application of Polymorphism

In object-oriented programming, polymorphism is mainly reflected through method overloading and coverage. The method is to send the same information to different objects and perform different tasks according to different objects.

3. constructor and Polymorphism

A class can have multiple constructor methods. When multiple class objects are created using different constructor methods of the same class, multiple objects are displayed, which reflects polymorphism.

4. Examples of multi-state application:

Design the program to explain the languages of several major countries in the world:

 //  Defined  Public   Class  People {  Public   Void  Speak () {system. Out. println ( "People speak" );}}  //  Define Chinese  Public   Class  Chinese extends people {  Public   Void  Speak () {system. Out. println ( "Chinese speak Chinese" );}}  //  Define Americans  Public   Class  American extends people { Public   Void  Speak () {system. Out. println ( "Americans speak English" );}}  //  Define Japanese  Public   Class  Japanese extends people {  Public   Void  Speak () {system. Out. println ( "Japanese speak Japanese" );}}  //  Define applications Public   Class  Yingyong {  Public   Static   Void  Main (string [] ARGs ){
People les; Chinese = New Chinese (): // Create a Chinese American = New American (): // Create Americans Japan =New Japan (); // Create Japanese Les = Chinese; Les. Speak (); Les = American; Les. Speak (); Les = Japan; Les. Speak ();}}

Finally, the result of running the program is:

Chinese speaking

Americans speak English

Japanese speak Japanese

Therefore, this fully reflects the behavioral polymorphism, also makes the program level clear, in line with the object-oriented programming philosophy!

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.