1. Inherit
1.1 characteristics
1. Establish inheritance relationship through the extends keyword
2. Java only supports single inheritance (a subclass can only have one parent), and inheritance can be passed (grandpa - > dad - > grandson)
3. Functions unique to subclasses, which cannot be used by parent classes
4. The child class cannot use the private resources of the parent class because the external class of the private resources is invisible; But be sure to note that inheritance is equivalent to a subclass copying a copy of the parent class's resources, including private resources
5. The subclass must be a type subordinate to the parent class, which is highly dependent and coupled
1.2. Exercise: inherit the introductory case
package cn.tedu.oppextends; /*This class is used for inheritance entry cases*/ public class TestExtendsDemo { public static void main(String[] args) { Animal animal = new Animal(); Cat cat = new Cat(); DingDang dingDang = new DingDang(); /*2,After a subclass inherits the parent class, it can use the functions of the parent class Inheritance can be passed on. Grandpa's function is passed on to Dad, and dad's function is passed on to his son java Only single inheritance is supported. A subclass can only have one parent class, but a parent class can have multiple subclasses * */ /* 5,Inheritance is a kind of is a relationship. Kittens are small animals and DingDang is kittens The subclass must be a type subordinate to the parent class, which is highly dependent and coupled * */ animal.eat(); cat.eat(); dingDang.eat(); dingDang.show(); /*3,The unique function of the subclass, which cannot be used by the parent class*/ } } /*1,We use the extends keyword to establish an inheritance relationship between the subclass and the parent class. The subclass extends the parent class*/ //1. Create a grandfather animal class class Animal { public void eat() { System.out.println("Small animals can eat anything~"); } } //2. Create a father class - kitten class class Cat extends Animal { int a = 10; //The access controller can only control visibility, and private resources can only be accessed in this class private int b = 100; public Cat(int a, int b) { System.out.println(""); } public Cat() { } } //3. Create a grandson - DingDang class DingDang extends Cat { public void show() { System.out.println(a); /*4,Subclasses cannot use the private resources of the parent class because the private resources are invisible to the external class But be careful: inheritance is equivalent to a subclass copying the resources of the parent class, including private resources * */ //System.out.println(b); System.out.println("Let me show you my magic pocket"); } }
2. super keyword
2.1 usage of super
When the parent and child class member variables have the same name, it is used when calling the parent class member variable with the same name
It can be understood as Father super=new Father(); Call the properties of the parent class through super
package cn.tedu.oppextends; /*This class is used to test the usage of inheritance*/ public class TestExtends1 { public static void main(String[] args) { new Son().eat(); } } class Father{ int sum=10; int a=999; } class Son extends Father{ int sum=100; public void eat(){ int sum=1000; System.out.println(sum); System.out.println(this.sum);//This represents this class and points to member variables /*super Keyword represents the parent class Father super=new Father(); * Therefore, when you want to specify a parent class member variable with the same name, use super to specify it * */ System.out.println(super.sum); System.out.println(a);//Therefore, this and super are used when variables have the same name, and a is called directly without the same name } }
3. Override (@ Override)
Two are the same, two are small and one is large. Verify the rewriting with @ Override annotation
package cn.tedu.oopextends; /*This class is used to test the use of methods in inheritance*/ public class TestExtends2 { public static void main(String[] args) { Son2 son2 = new Son2(); son2.eat(); son2.play(); new Father2().eat(); new Father2().play(); } } /* OCP Principle: facing function modification and closing, facing function expansion and opening For example, in rewriting, we need to update the function without modifying the parent method * */ /*Can a subclass Override the private method of the parent class? No, add @ Override to verify whether the method has been overridden*/ class Father2{ public void eat(){ System.out.println("Dad loves meat~"); } public void play(){ System.out.println("Love flying kites~"); } } class Son2 extends Father2{ /*1.Method Rewriting: if the subclass is not satisfied with the method of the parent class, it can be overridden*/ @Override public void eat(){ System.out.println("My son likes vegetables~"); } /*2.Subclasses can have their own unique functions -- new functions*/ public void work(){ System.out.println("We are all programmers"); } /*3.Rule of method Rewriting: two identical, two minor and one major principle The two are the same: the method name & parameter list (method signature) is consistent. play() Two small: return value type of subclass override method < = return value type of parent class method Exception type thrown by subclass method < = exception type thrown by parent class One: modifier of subclass overriding method > = modifier of parent class method public **/ @Override /*Annotation, small tag / tag, used to mark this is an rewritten method*/ public void play(){ System.out.println("Love playing games~"); } }
4. Calls to constructor methods in inheritance
1. Constructor cannot be inherited;
2. The first line of subclass construction method has super() by default;
3. When a subclass creates an object, it must first call the constructor of the parent class. You can also customize super() by default; 😉;
4. Constructor cannot be inherited because the constructor name must be the same as the class name
package cn.tedu.oopextends; /*This class is used to test the constructor used to construct methods in inheritance*/ import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput; public class TestCons { public static void main(String[] args) { Father father = new Father(); Son son = new Son(); Son son1 = new Son("Big head son"); } } class Father{ public Father() { System.out.println("Parameterless construction of parent class"); } public Father(int n) { System.out.println("Parametric construction of parent class"+n); } } /*3.Constructor cannot be inherited because the constructor name must be the same as the class name*/ class Son extends Father{ public Son() { /* 1.When a subclass creates an object, the constructor of the parent class will be called by default The first line in the subclass construction method. Super () exists by default; - > Call the parameterless construction of the parent class*/ /*2.When a subclass creates an object, it must first call the constructor of the parent class As for what kind of construction to call the parent class, I don't care. As long as there is a parent class construction to call * */ super(); System.out.println("Nonparametric construction of subclasses"); } public Son(String s){ super(3); System.out.println("Parametric construction of subclasses"+s); } }
5. Expand
5.1 difference between this and super
1. This is to call this class, and super is to call the parent class
2. Both this and super are used when the variable name is the same
3. The premise of calling super is to have inheritance relationship
4. When a subclass inherits the parent class, the first line of the subclass's construction method defaults to super();
5.2 difference between overloading and overriding
Overload:
1. Overloading is the existence of multiple methods with the same method name but different parameter lists in a class
2. Benefits: call different methods according to different parameters entered by the user (convenient for consumers), reflecting the flexibility of the program
rewrite:
1. Overriding is to modify the original function of the parent class after inheriting the child class
2. Meaning: extension and modification of functions without modifying the source code