Inheritance in Java refers to the abstraction of a set of classes to better model the real world. I actually prefer to describe this relationship as a kind of reification, where a subclass is a reification of the parent class, and a subclass can call all the methods and properties in the parent class except for the private methods and properties.

  1. Extends means “to extend”. A subclass is an extension of a parent class.
  2. Classes in JAVA have single inheritance, not multiple inheritance.
  3. Inheritance is a relationship between classes. In addition, classes and classes directly related dependencies, composition, aggregation, and so on.
  4. Two classes of an inherited relationship, one is a subclass (derived class) and one is a parent class (base class). Subclasses inherit from their parent class and are represented by the extends keyword.
  5. There should be, in a sense, an “is A “relationship between subclasses and their parents.
  6. Object class
  7. super
  8. Methods to rewrite
  9. The super keyword, which calls methods and properties in the parent class.
  10. This keyword, calling its own current class methods and properties.

The parent class

package com.encapsulation.demo01;

// CTRL +h to view the inheritance tree
public class Person {
    / / public public
    / / protected protection
    / / the default default
    / / private private

    private int money = 10_0000_0000;
    
    // Define a no-parameter construct in the parent class
    public Person(a) {
        System.out.println("Person executed");
    }
    
     // Private cannot be inherited to subclasses
    private void pri(a) {
         System.out.println("Private");
     }

    public void say(a) {
        System.out.println("Said a word.");
    }

    public int getMoney(a) {
        return money;
    }

    public void setMoney(int money) {
        this.money = money; }}Copy the code

A subclass

package com.encapsulation.demo01;

// The student class inherits from the human-derived class
// Subclasses inherit from the parent class and have all the methods of the parent class with the prefix pubilc
All classes in Java inherit directly or indirectly from the Object class

public class Student extends Person {
    
    // The subclass also defines a no-argument construct
    public Student(a) {
        super(); // The constructor that calls the parent class must be placed on the first line, where super defaults to calling the parent class with no arguments
        System.out.println("Execute Student");
    }
    
     public void test(a) {
        super.say(); // Use super to call the method of the parent class}}Copy the code

perform

package com.encapsulation;

import com.encapsulation.demo01.Student;

public class App {

    public static void main(String[] args) {
        Demo01 s1 = new Demo01();
        
        // New student defaults to executing the parent class's construct while executing its own construct
        Student student = newStudent(); }}Copy the code

A subclass

package com.encapsulation.demo01;

/ / a derived class
public class Teacher extends Person {
    
}
Copy the code

Pay attention to the point

  1. Super calls the constructor of the parent class, which must be the first constructor
  2. Super must only appear in methods or constructors of subclasses
  3. Super and this cannot call the constructor at the same time

Vs this

Different objects are represented:

  1. This: calls the object itself

  2. Super: indicates the application of the superclass object

  3. Premise -this: can be used without inheritance. -super: can be used only under inheritance conditions

  4. A constructor

    • this(); The construction of this class
    • Super; The construction of the parent class

The difference between the construction method and the common method inside knowledge point class

  1. Constructor parameterless constructor and parameterless constructor. Each class must have one parameterless constructor or it will not be instantiated

  2. Multiple parameter constructs can overload methods, and each parameter type must be inconsistent

  3. Constructor names are the same as class names

// Overloaded parameter constructors must have different parameter types
public Student(String name) {
    System.out.println(name);
}

// there is a parameter constructor
public Student(int age) {

}




Student student = new Student(12); // The data type is used to determine which parameter construct to execute
Copy the code

conclusion

  1. Superclass private properties and methods cannot be inherited from subclasses
  2. Subclasses call methods and properties of their parent class through super
  3. When both the parent class and the subclass have no argument constructor, the new subclass will call super() by default.

In the end, I wish you all success as soon as possible, get satisfactory offer, fast promotion and salary increase, and walk on the peak of life. If you can please give me a triple support for me yo, we will see you next time