1. Inheritance (extends)
1.1 Inheritance : Genetics
1.2 Syntax
public class subclass extends parent class {
}
public class Dog extends pet{
}
1 public classPet {2 PrivateString name = "anonymous";3 Private intHealth = 90;4 Private intLove = 0;5 publicPet () {6 this. Health = 95;7System.out.println ("non-parametric Construction method for performing pets");8 }9 publicPet (String Name) {Ten this. Name =name; one } a publicString getName () { - returnname; - } the public intgethealth () { - returnhealth ; - } - public intgetlove () { + returnlove ; - } + public voidji () { aSystem.out.println ("confession of the pet: \ n my name" + this. name+ ", Health Value is" + this. health at+ ", and the intimacy of the host is" + this. love+ ". "); - } -}
Pet Class
1 public classDogextendspet{2 PrivateString strain;3 publicDog (String name,string strain) {4 //Super (name);5 this. Strain =strain;6 }7 publicString getstrain () {8 returnstrain;9 }Ten public voidji () { one Super. Ji (); aSYSTEM.OUT.PRINTLN ("i am a" + this. strain); - } -}
Dog class inherit pet class
1 /*2 * Inheritance Syntax3 * Modifier Subclass extends superclass{4 * Class Definition section5 * }6 * Inheritance is implemented by the extends keyword, where subclass is a subclass, superclass is the parent class7 * modifier If it is public, the class is visible throughout the project, and if there is no public modifier, the class is visible only in the current package8 * Cannot use private and protected to decorate class9 */Ten public classPenguinextendsPet { one PrivateString sex; a //final String home = "antarctic"; - //public void Sethome (String Home) { - //this.home = home;//home cannot be assigned again the // } - publicPenguin (String name,string Sex) { - Super(name); - this. Sex =sex; + } - publicString getsex () { + returnsex; a } at public voidji () { - Super. Ji (); -System.out.println ("sex is:" + this. sex); - } -}
Penguin class inherit pet class
1 /*2 * Methods of rewriting3 * A method that inherits from a parent class can be rewritten in a subclass as required4 * Requirements: overriding methods and overriding methods must have the same method name5 * must have the same parameter list6 * The return value type of the overridden method must be the same as the return value type of the overridden method7 * Override method does not narrow access to overridden methods8 */9 public classTEXTDPP {Ten public Static voidmain (string[] Args) { onePet Pet =NewPet ("kat"); a Pet.ji (); -Dog dog =NewDog ("dog", "snow Wolf")); - Dog.ji (); thePenguin PGN =NewPenguin ("chubby", "q Sister")); - Pgn.ji (); - } -}
Test Class
The result of the Operation Is:
1.3 Characteristics of the inheritance
A. Subclasses can inherit the Non-corpse properties and methods of the parent class
B. Cannot inherit a constructor method
C. Methods and properties that inherit the default permission modifier, subclasses and parent classes must be in the same package
1.4 Super (parent Class)
Super. Property Name//representation Method The property of the parent class
Super. Method Name (parameter);//method that represents access to the parent class
Super. (parameter);//the constructor that accesses the parent class
note: When Super () accesses the parent class constructor, it must be written in the first row
note: code blocks are executed before the constructor
2. rewrite
2.1 Overrides are subclasses overriding methods that define the parent class
2.2 Rewrite rules
A. Method name and parameter list are the same
B. Modifier access permissions subclasses are the same as the parent class or larger than the parent class
C. The return value type subclass is the same as the parent class (or subclass of the parent class return value Type)
D. The exception is the same as the parent class or has a smaller scope
general: When overridden, the Subclass's things are consistent with the parent class
Construction method cannot be overridden
cannot be overridden because it cannot be inherited
3. Abstract class
3.2. abstract Methods
Syntax: public abstract return value type method name (parameter list);
Example: public abstract void print ();
Note: The abstract method has an abstract modification, and the abstract method has no method body
1 public classFishextendsanimal{2 intWeight = 5;3 intAge = 9;4 public voidInfo () {5SYSTEM.OUT.PRINTLN ("i am a" +weight+ "fish!) ");6System.out.println ("+age+" Year old!) ");7 }8 9}
fish inherit animal class
1 public Abstract class Animal {2 int age ; 3 String color = ""; 4 int weight; 5 Abstract void // Abstract This method allows subclasses to implement 6 }
Animal Abstract class
1 public classBirdextendsanimal{2String color= "red";3 intAge = 4;4 public voidInfo () {5SYSTEM.OUT.PRINTLN ("i am a" +color+ "bird!) ");6System.out.println ("+age+" Year old!) ");7 }8 9}
birds inherit animal class
1 public class TEXTBFA { 2 Public static void main (string[] Args) { 3 Bird b= new Bird (); 4 B.info (); 5 Fish f = new Fish (); 6 F.info (); 7 } 8 }
Test Class
The result of the Operation Is:
4. Characteristics of abstract classes and abstract methods
A. Both are modified with an abstract
B. Abstract classes cannot be instantiated
And the subclass that inherits the abstract class must override all abstract methods of the abstract class
note:1) Abstract classes are not necessarily abstract methods
2) contains classes in abstract methods, must be abstract (interface)
Java (14) Inheritance