Java polymorphism (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/11/26/2789638.html

Polymorphism, defined in the dictionary, refers to the fact that organisms or species in biology can have many different forms or stages. This principle also applies to object-orientedProgramming LanguageFor example, Java. Sub-classes can define their own unique behaviors and share some of the same features of the parent class.

Polymorphism can be displayed by modifying the bicycle class. For example, you can add a printdescription method to display all data in the current instance.

 
Public VoidPrintdescription () {system. Out. println ("\ Nbike is" + "in gear" +This. Gear+ "With a cadence of" +This. Cadence + "and traveling at a speed of" +This. Speed + ".");}

To demonstrate the Java language polymorphism, we have extended the bicycle class: mountainbike and roadbike classes. for example, mountainbike, add a variable suspension, which is a string, and its value if it is front indicates that this vehicle has a front shock absorber, if it is dual, it indicates that the bicycle has a front rear shock absorber.

The updated class is as follows:

 Public   Class Mountainbike Extends  Bicycle {  Private  String suspension;  Public  Mountainbike (  Int  Startcadence,  Int  Startspeed, Int  Startgear, string suspensiontype ){  Super  (Startcadence, startspeed, startgear );  This  . Setsuspension (suspensiontype );}  Public  String getsuspension (){  Return   This  . Suspension ;}  Public   Void Setsuspension (string suspensiontype ){  This . Suspension = Suspensiontype ;}  Public   Void  Printdescription (){  Super  . Printdescription (); system. Out. println ( "The" + "mountainbike has a" + Getsuspension () + "Suspension ." );}} 

Note that the printdescription method has been overloaded. In addition to the previous information, the additional susion sion information will also be included in the output.

Next, create a roadbike class. Because road bikes or racing bicycles have fine tires, add an attribute to track the bandwidth of the tires. Here is roadbike'sCode:

 Public   Class Roadbike Extends  Bicycle {  //  In millimeters (mm)      Private   Int  Tirewidth;  Public Roadbike ( Int  Startcadence,  Int Startspeed,  Int  Startgear,  Int  Newtirewidth ){  Super  (Startcadence, startspeed, startgear );  This  . Settirewidth (newtirewidth );}  Public   Int  Gettirewidth (){  Return   This . Tirewidth ;}  Public   Void Settirewidth ( Int  Newtirewidth ){  This . Tirewidth = Newtirewidth ;}  Public   Void  Printdescription (){  Super  . Printdescription (); system. Out. println ( "The roadbike" + "has" + gettirewidth () + "mm tires ." );}} 

Note that the printdescription method has been overloaded. This time, the width of the tire is displayed.

To sum up, there are three types: bicycle, mountainbike, and roadbike. The two subclasses reload the printdescription method and output unique information.

Here is a testProgramCreate three bicycle variables. Each variable is assigned to each instance of the three bicycle classes, and each variable is output.

Public ClassTestbikes {Public Static VoidMain (string [] ARGs) {bicycle bike01, bike02, bike03; bike01=NewBicycle (20, 10, 1); Bike02=NewMountainbike (20, 10, 5, "dual"); Bike03=NewRoadbike (40, 20, 8, 23); Bike01.printdescription (); bike02.printdescription (); bike03.printdescription ();}}

The program output is as follows:

Bike is in gear 1 with a cadence of 20 and traveling at a speed of 10. Bike is in Gear5 with a cadence of 20 and traveling at a speed of 10. The mountainbike has a dual suspension. Bike is in Gear8 with a cadence of 40 and traveling at a speed of 20. The roadbike has23mm tires.

The Java Virtual Machine (JVM) does not call the class definition method of the object instance, but calls the appropriate method of the object instance. This behavior is called a virtual method call to demonstrate the important characteristics of polymorphism in Java.

 

 

Related Article

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.