"Java from Beginner to Mastery" chapter eighth study notes

Source: Internet
Author: User

8th Chapter Interface, inheritance and polymorphism

First, the use of the interface

1, the definition of the interface, the basic syntax format is as follows:

interface Interface Name [extends  parent interface list]{        [public] [static] [final ] ] variable;        [Public] [abstract] method;} /* 1, modifiers are optional parameters, specify the access rights of the interface, the optional value is public or omit the default 2, the interface name is a comparison parameter, must be a valid Java Identifier 3, the extends parent interface list is an optional parameter, is used to describe the interface to be defined inherit from which parent interface. 4, Method: interface Definition of the method is defined without a specific implementation of the code * /
 Public Interface The icalculate{// Interface name must be the same as the interface file name    finalfloat pi=3.14159f;  // defining the Pi constants    float getarea (float R);   // Define calculated area method (but not specifically)    float getcircumference (float R); // defines the calculation perimeter method (but not specifically implemented)}

2, the implementation of the interface
After the interface is defined, it can be implemented in the class that inherits the interface, and the inherited interface must be represented by the Implements keyword. For the above example, the implementation code for a specific class is as follows:

1  Public classCire Implement icaculate{2      Public floatGetarea (floatR) {3         floatarea=pi*r*R;4         returnArea ;5     }6      Public floatGetcircumference (floatR) {7         floatcircumference=2*pi*R;8         returncircumference;9     }Ten}

Ii. Inheritance of classes
1, the implementation of the inheritance

[Modifier] Class Subclass name extends parent class name {
...
}

1 //Example2  Public classbird{3String color= "White";4String skin= "Feather";5 }6  Public classPigeonextendsbird{7      Public Static voidMain (string[] args) {8Pigeon pigeon=NewPigeon ();9 System.out.println (pigeon.color);Ten}

2. When some methods in the parent class do not meet the requirements of the child class, the subclass will override the method with the same name.

1 //Example Bird.java2  Public classbird{3String color= "White";4String skin= "Feather";5      Public voidcry () {6SYSTEM.OUT.PRINTLN ("Birdsong!"));7     }8 }9 //Pigeon.javaTen  Public classPigeonextendsbird{ One      Public voidcry () { ASystem.out.println ("The Dove is barking!")); -         } -      Public Static voidMain (string[] args) { thePigeon pigeon=NewPigeon (); - pigeon.cry (); -     }    -}

Three, polymorphic
The polymorphism of a class is usually achieved by overloading (overloading) and rewriting (overriding) of the method. The override is the override of the method with the same name as the parent class in the subclass described above. Overloading refers to the method in which multiple method names are the same in the same class, but the number of arguments or the different types of parameters (as described in the previous construction method, the non-parametric constructor method and the parameter constructor method are also overloaded), and when the program executes, it will automatically match the method according to the number and type of the calling parameters.

/*defines a class named calculate that defines two Getarea methods * and two draw methods in a class*/ Public classCalculate {Final floatpi=3.14159f; //Calculate Circle Area     Public floatGetarea (floatR) {        returnpi*r*R; }    //Calculate Rectangular Area     Public floatGetarea (floatLengthfloatwidth) {        returnlength*width; }    //Draw num shapes in any shape     Public voidDrawintnum) {System.out.println ("Draw" +num+ "a graphic! "); }    //draw a graphic of the specified shape     Public voidDraw (String shape) {System.out.println ("Draw a" +shape+ "! "); }     Public Static voidMain (string[] args) {Calculate pic=NewCalculate (); floatL=20f; floatw=15f; floatarea1=Pic.getarea (L,W); System.out.println ("Rectangular area is" +area1); floatr=9.12f; floatArea2=Pic.getarea (R); System.out.println ("Circular area is" +area2); intNum=7;        Pic.draw (num); Pic.draw ("Irregular shape"); }}

"Java from Beginner to Mastery" chapter eighth study notes

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.