Inheritance of object-oriented features in java

Inheritance:

Extract the same content of multiple classes into another class, and then use the keyword extends to implement inheritance

Benefits of inheritance:

1. Improve the reusability of the code

2. The maintainability of the code is improved. Only the content of the parent class needs to be modified

3. Create inheritance relationship between classes to pave the way for later polymorphism. Polymorphism can only be done with inheritance

Disadvantages of inheritance:

1. Class coupling is enhanced. When a parent class changes, the child class also changes

2. Only a single inheritance is allowed, not multiple inheritance. Multiple inheritance is allowed. The child inherits the father, and the father inherits the grandfather

//Multi level inheritance
class GrandFather{
}
class Father extends GrandFather{
}
class Son extends Father{
}
//Multiple inheritance is not allowed, such as
class Son extends Father,Mother{
}

Characteristics of inheritance:

1. If you want to initialize a subclass, you must initialize the parent class first. Only a father can have a son

2. A subclass can only inherit non private members of the parent class,

It can be understood through the code and comments of the following case

class Father2{
    int num = 20;
    Father2(){
        System.out.println("This is the father's parameterless construction method");
    }
    private void fun1(){
        System.out.println(num);
    }
}
class Son2 extends Father2 {
    public void fun2() {
        System.out.println(num);
//        System.out.println(num2);
//        A subclass cannot inherit private member variables of a parent class
    }
}
public class ExtendsDemo2 {
    public static void main(String[] args) {
        Son2 son2 = new Son2();
        son2.fun2();//The construction method of the father is also executed. To initialize the subclass, you must first initialize the parent class
        //Running result: This is the father's parameterless construction method, 20
        //son2.fun1();
        //Private member methods of the parent class cannot be accessed
    }
}

3. A subclass cannot inherit the construction method of the parent class. The construction method belongs to the class itself, but the construction method of the parent class can be accessed through the super keyword, because to initialize a subclass, the parent class must be initialized first, which is initialized through the construction method

4. Do not use inheritance for some functions. When two class grammars meet what is what, you can use inheritance. For example, dogs, cats and tigers are animals.

Relationship between inheritance and member variables:

a. When the member variable of the child class has the same name as the member variable in the parent class,

Find: 1 First, search within the local scope of the method. If it is found, it returns

2. If the local scope of the method cannot be found, go to the member location (member variable) of this class to find it. If it is found, it will return

3. If it cannot be found in the member location, go to the parent member location to find it. If it is found, it will return

4. If the parent member cannot be found, an error is reported

b. When the name of the member variable of the child class is different from that of the parent class: whoever the name is will access who it is

The difference between this keyword and super keyword

Difference: this represents the current object calling the class, and super represents the identification of the storage space of the parent class (the reference of the parent class can operate on the members of the parent class)

usage method:

1. Access the member variable this The member variable accesses the member variable in this class, super Member variables access member variables in the parent class

//Use this and super keywords to access member variables in this class and parent class
class Father3{
    int num= 10;
}
class  Son3 extends Father3{
    int num = 20;
    public void show(){
        int num= 30;
        System.out.println(num);
        System.out.println(this.num);
        System.out.println(super.num);
    }
}
public class ExtendsDemo3 {
    public static void main(String[] args) {
        Son3 son3 = new Son3();
        son3.show();
    }
}

2. Access construction method

To initialize a subclass, you must first initialize the parent class, because the subclass will inherit the data of the parent class and may even use the data of the parent class

Data, so the parent class must be initialized before the child class is initialized

Note: the first sentence of the constructor of each subclass defaults to super()

//This case will first access the parameterless construction method of the parent class, and then access the parameterless construction method of the child class
class Father8 {
    int age;
    Father8() {
        System.out.println("Parameterless constructor in parent class");
    }
    Father8(String s) {
        System.out.println("This is the construction method of the parent class with parameters" + s);
    }
}
class Son8 extends Father8 {
    Son8() {
        System.out.println("Parameterless construction of subclasses");
    }
    Son8(String s) {
        System.out.println("Construction method of subclass with parameters" + s);
    }
}
public class ExtendsDemo7 {
    public static void main(String[] args) {
        Son8 s1 = new Son8("Xiao Wang");
    }
}

The execution result of the above case will first access the parameterless construction method of the parent class, and then access the parameterless construction method of the child class, because there is a hidden super(); in the parameterless construction method of the child class;, It will guide us to access the parameterless constructor of the parent class first. If we want to access the parameterless constructor of the parent class, we need to write a super (parameter); To direct access to the parameterized constructor of the parent class.

3. Method of accessing members: this Member method (), super Member method ()

1. When the member method name of the subclass is different from that of the parent class, the caller will call the method name of the caller

2. What happens when the member method name of the child class is the same as that of the parent class? (principle of proximity)
1) now search in this class. If there is, call it. If there is no search in the parent class,
2) if there is a method in the parent class, the method of the parent class will be called
3) if there is no method name to call in the parent class, an error will be reported, indicating that the method cannot be found.

Method override:

If the method declaration of the subclass is the same as that of the parent class, this phenomenon is called method rewriting. Rewriting occurs in the inheritance relationship. The method declaration of the subclass is the same as that of the parent class, but the implementation results are different. For example, I write a study method in the parent class, which outputs "learning java", and a study method with the same declaration in the subclass, which outputs "learning advanced mathematics". Here, although the declaration of the parent class is the same as that of the subclass, our final implementation is different, which is called method rewriting.

Keywords: Java Back-end

Added by undecided name 01 on Thu, 20 Jan 2022 01:20:04 +0200