1. Usage scenarios of inheritance

  1. The father ate and slept every day. He gave birth to two sons who inherited his father’s two points and also ate and slept every day.

  2. The father is a farmer and his work is farming every day. His two sons are very successful, one is a teacher and the other is a doctor.

2. Implementation of inheritance

The extends keyword implements an inheritance relationship between two classes

public class A {}
public class B extends A {}
Copy the code

3. Member variable access in inheritance

/ / parent class
public class Employee {
    int num = 10;
    int age = 100;
}

/ / subclass
public class Student extends Employee {
    int num = 20;
}

/ / test
public class FieldVarTest {
    public static void main(String[] args) {
        // A member variable with the same name as the parent and subclasses
        Employee employee = new Student();
        Student student = new Student();
        System.out.println(employee.num); / / output 10
        System.out.println(student.num); / / output 20
        // Only the parent class has member variables
        System.out.println(student.age); // take the parent class and print 100}}Copy the code

When accessing a member variable with the same name as a parent or child class, take the value defined in the class to the left of the equals sign of the created object; Value to the parent class when a member variable accessed through a subclass object exists only in the parent class.

Constructor access in inheritance

/ / parent class
public class Employee {

    public Employee(a) {
        System.out.println("Parent class constructor");
    }

    public Employee(int num) {
        System.out.println("Parameter constructor for parent class"); }}// Subclass constructor
public class Student extends Employee {

    public Student(a) {
        // TO DO
        System.out.println("Subclass Constructor"); }}/ / test
public class ExtendsConstructorTest(String[] args) {
    Student student = new Student();
    // There are three possible ways TO DO this
    // 1. Do not write -> Call super() by default
    // 2. Super () -> call super()
    // 3. super(10) -> call the superclass overload constructor
}
Copy the code

Note that the superclass constructor must be the first sentence of the subclass constructor. If no superclass constructor is specified, super() is executed by default. If so, an overloaded superclass constructor, such as super(10), is executed.

5. Member method overrides in inheritance

Overrides note that the subclass has the same legal name and argument list as its parent class; Subclass return type <= parent class return type; Subclass permissions >= superclass permissions.

/ / parent class
public class Employee {
    
    public void work(a) {
        System.out.println("I'm going to work."); }}/ / subclass
public class Student extends Employee {

    @Override
    public void work(a) {
        System.out.println("I'm going to school."); }}/ / test
public class SameNameVarTest {
    
    public static void main(String[] args) {
        Employee employee = new Student();
        employee.work(); // Output "I went to school"}}Copy the code

Here is an example of a rewrite, as you can see, to change the content of a method with the same name as a parent class based on the characteristics of the subclass. If we have more than one child class, we can override the work method. For example, if we create a new child class, we can change the work method to system.out.println (” I’m going to school “). . Another thing worth noting in this example is the Employee Employee = new Student(); We refer the parent class variable to the subclass object. When we call the work method, we call the subclass method, but when we inherit the variable access, we call the parent class variable. This is another characteristic of Java, polymorphism, lively description of what this post is a polymorphic: www.zhihu.com/question/30…

6. Inherit the super and this keywords

/ / parent class
public class Employee {
    
    int num = 10;

    public Employee(a) {
        System.out.println("Parent class constructor");
    }

    public void work(a) {
        System.out.println("I'm going to work."); }}/ / subclass
public class Student extends Employee {

    int num = 20;

    public Student(a) {
        super(a);// super is used to access the superclass constructor through super() in the subclass constructor
    }
    
    public Student(int id) {
        this(a);The first use of this is to access an overloaded constructor in a subclass constructor through this()
    }

    public void superTest(a) {
        System.out.println(super.num); Super.num is used to access the superclass member variable in the subclass member method
		super.work(); Super.work () is used to access the member methods of the subclass
    }
    
    public void thisTest(a) {
        System.out.println(this.num); // this is used in the subclass member method to access the subclass member variable
        this.work(); Use this.work() to access a subclass member method
    }
    
    public void work(a) {
        System.out.println("I'm going to school."); }}Copy the code

When using the super and this keywords, note that in the subclass constructor, both super() and this() appear only in the first sentence of the constructor, meaning that they cannot be used in the same constructor at the same time.

To explain super and this more clearly, let’s go through a code that interacts with memory at runtime.

/ / the parent class (1)
public class Employee {
    
    int num = 10;
    
    public void work(a) {
        System.out.println("I'm going to work."); }}/ / subclasses (1)
public class Student extends Employee {

    int num = 20;
    
    public void getVariable(a) {
		System.out.println(super.num);
        System.out.println(this.num);

    }
    public void work(a) {
        super.work(); }}// Test class (1)
public class SuperThisTest {
    
    public static void main(String[] args) { / / (2)
        Student stu = new Student(); / / (3)
        stu.getVariable(); / / (4)
        stu.work(); / / (5) (6)}}Copy the code

(1) Save the. Class files of the parent, subclass, and test classes to the method section, where the subclass has a super flag pointing to the parent class.

(2) Push the main method and start executing the contents of the method.

(3) Create Student on stack, Employee, Student, stu, stu, stu, stu, stu, stu, stu, stu, stu, stu, stunew Student()Object.(4) Execute the main methodstu.getVariable();Statements,getVariable()Method stack, executes the contents of the method, throughsuper.numFind num in the Employee content and passthis.numFind num in Student,getVariable()After execution, exit the stack.

(5) Execute stu.work() in main; Super.work (); super.work(); Find the work() method for Employee and push it.

(6) Execute system.out.println (” I’m at work “); , the work() method corresponding to Employee completes execution and exits the stack; Student = work(); Student = work(); The main method completes and exits the stack.

7. Inherit special features in Java

(1) A class can have only one direct parent, that is, only single inheritance is supported.

(2) A parent class can also have its own parent class, which supports multi-level inheritance.

(3) A parent class can have more than one subclass.