First, inheritance
1. Concept of inheritance
Inheritance solves code reuse and allows our programming to be closer to human thinking. When multiple classes have the same attributes (variables) and methods, you can abstract the parent class from these classes, define the same properties and methods in the parent class, and all subclasses do not need to redefine the properties and methods, just declare the inherited parent class through the extends statement. The syntax is as follows:
Class Subclass extends Parent class
In this way, subclasses automatically have certain properties and methods defined by the parent class. In addition, not all properties of the parent class, methods can be inherited by the quilt class. The properties and methods of the public modifier of the parent class, the properties and methods of the protected modifier, the default modifier properties and methods can be inherited by the quilt class, and the properties and methods of the private modifier cannot be inherited by the quilt class.
2. Case:
//function: Explain the importance of inheritance PackageCom.davperson;//Package Name Public classDemo117 { Public Static voidMain (string[] args) {pupil P1=Newpupil (); P1.printname (); }}//extract the student's common attributes and make a parent classclassstu{//Defining member Properties protected intAge ; PublicString name; Public floatfee; PrivateString job;//private will not be inherited//programming, if you don't want subclasses to inherit a property or method//you can declare it as private Public voidPrintname () {System.out.println ("First Name" + This. Name); }}//Pupil classclassPupilextendsstu{//Pay Tuition Public voidPayfloatfee) { This. fee=fee; }}//ToddlerclassPreextendspupil{//Pay Tuition Public voidPayfloatfee) { This. fee=fee*1.5f; }}//Middle School ClassclassMiddlestuextendsstu{//Pay Tuition Public voidPayfloatfee) { This. fee=fee*0.8f; }}//Undergraduate ClassclassColstuextendsstu{//Pay Tuition Public voidPayfloatfee) { This. fee=fee*0.1f; }}
3. Inheritance--Precautions
1, subclasses can inherit at most one parent class (refer to direct inheritance)
2, Java All classes are subclasses of the object class (all subclasses can be inherited in succession, for example: Sun------son)
3. There are 202 packages in JDK6 3,777 classes, interfaces, exceptions, enumerations, annotations, and errors
4, in the development of the time, we strongly recommend that you check the JDK Help document
5, in the use of classes, really do not know how to do, more use of search engines
Thus, the definition of a class can be further improved:
Package package name; class extends Parent Class { member variable; construction method; member method;}
Ii. method overloading and method overrides
Before you explain polymorphism, you must explain method overloading (overload) and method overrides (override).
1. method Overloading (overload)
Simply put: A method overload is a variety of implementations of the same function in a class, whichever method is used, depending on the parameters given by the caller.
Case:
/*method Overloading (overload) Getmax writes a class (ABC) that can be written to receive two integers, returning a larger number of two numbers [Demo119.java]*/ Public classdemo119{ Public Static voidMain (String []args) {ABC2 ABC1=NewABC2 (); System.out.println (Abc1.getmax (12,14)); System.out.println (Abc1.getmax (24f,20f)); }}classabc{//returns a large integer Public intGetmax (intIintj) { if(i>j) { returni; }Else{ returnJ; } } //returns the number of large float types Public floatGetmax (floatAfloatb) { if(a>b) { returnA; }Else{ returnb; } }
For more instructions on method overloading:
1. Same method name
2, the method parameter type, the number, the order has at least one different
3. The method return type can be different (only the return type is not the same and cannot form an overload)
4. The modifier of the method can be different (just control the access modifier, cannot form the overload)
2. Method override (Override)
Method overrides are subclasses that have a method, like the name, return type, and parameters of a method of a parent class, then we say that this method of the subclass overrides that method of the parent class. In the following example, the Cry method in the Cat class overrides the Cry method of the animal class.
//Subclass method Overrides parent class method [Demo120.java] Public classdemo120{ Public Static voidMain (String []args) {//Create a catCat cat1=NewCat (); Cat1.cry (); Dog Dog1=NewDog (); Dog1.cry (); }}//Animal Classclassanimal{intAge ; String name; //will be called Public voidcry () {System.out.println ("I'm an animal, I don't know how to call it."); }}//cat Cat ClassclassCatextendsanimal{//overriding the parent class method Public voidcry () {System.out.println ("Cat barking!"); }}//Dog ClassclassDogextendsanimal{//overriding the parent class method Public voidcry () {System.out.println ("Bark!"); }}
For more instructions on method overrides:
Method coverage There are two points to be sure to note:
1, the method of the subclass of the return type, parameter, method name, and the parent class return type, parameter, method name exactly the same, or compile error.
2. The subclass method cannot reduce the access rights of the parent class method.