Class inheritance and use of the super keyword (JAVA)

Table of contents

inherit

super 

 Subclass Constructor

final


 

inherit

All OOP languages ​​will have three characteristics:

  1. Packaging (click to jump)
  2. inherit
  3. Polymorphism (click to jump)

Why is there inheritance? You can first look at the following example:

        The codes in the above two classes are very similar because only the last method is different, and the others are the same. This definition not only leads to code redundancy but also is very troublesome. In the OOP language, inheritance is specially used for commonality extraction and code reuse .


Inheritance (inheritance) mechanism : It is the most important means for object-oriented programming to make code reusable . It allows programmers to expand and add new functions on the basis of maintaining the characteristics of the original class, thus generating new classes, called derivation kind. Inheritance presents the hierarchical structure of object-oriented programming, reflecting the cognitive process from simple to complex. The main problem solved by inheritance is: the extraction of commonality and the realization of code reuse .

To achieve inheritance in JAVA, you need to use the extends keyword.

Modifier class subclass extends parent class { // ... }

At this point we are looking at the next example. Teachers and students belong to the big category of people, so we can define a People class separately to put their same attributes and methods together, and then the student class and the teacher class inherit this class respectively. Category:

class People{
    public String name;
    public int age;
    public void speak(){
        System.out.println(this.name+"正在说话");
    }
}

class Teacher extends People{
    public void work(){
        System.out.println(this.name+"正在教书");
    }
}

class Student extends People{
    public void study(){
        System.out.println(this.name+"正在学习");
    }
}
public class Test {
    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        teacher.name = "zhangsan";
        teacher.age = 24;
        teacher.speak();
        teacher.work();
        System.out.println("==========");
        Student student = new Student();
        student.name = "xiaoming";
        student.age = 15;
        student.speak();
        student.study();
    }
}

 Note: The subclass will inherit the member variables or member methods in the parent class to the subclass.

So now there is a new question, what will the system do if the subclass has the same properties or methods as the parent class?

class People{
    public String name;
    public int age = 25;
    public void speak(){
        System.out.println(this.name+"正在说话");
    }
}

class Teacher extends People{
    public int age = 20;
    public void work(){
        System.out.println(this.name+"正在教书"+"今年"+this.age);
    }
}

public class Test {
    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        teacher.name = "zhangsan";
        teacher.work();

    }
}

At this time, both the parent class and the subclass have the age attribute. After the code runs, you can see that the attribute of the subclass is used. 

 Summarize:

  1. When accessing the properties in the parent class and subclass through the subclass object , access your own first , and then look for it in the parent class if you don’t have it, and report an error if you don’t have it in the parent class.
  2. When accessing a method with a different name in the parent class and the subclass through the subclass object , first look for it in the subclass, and access it if you find it, otherwise look for it in the parent class, and access it if you find it, otherwise, compile and report an error.
  3. When accessing the method with the same name of the parent class and the subclass through the subclass object , if the parameter list of the method with the same name of the parent class and the subclass is different (overloading), select the appropriate method to access according to the parameters passed by the calling method, and report an error if there is none;

Even if there is a private modified attribute or method in the parent class, the subclass will inherit it but cannot access it.


super 

In the above example a new keyword super appears. So what does the super keyword do?

The main function of this keyword is to access the members of the parent class in the method of the subclass.

class People{
    public String name;
    public int age = 25;
    public void speak(){
        System.out.println(this.name+"正在说话");
    }
}

class Teacher extends People{
    public int age = 20;
    public void work(){
        System.out.println(this.name+"正在教书"+"今年"+super.age);
    }
}

public class Test {
    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        teacher.name = "zhangsan";
        teacher.work();

    }
}

Because both the parent class and the subclass have age attributes, if you want to access the properties of the parent class, you can use the super keyword, super+. to access the properties and methods of the parent class. (The third method of using super will be introduced below).


 

 Subclass Constructor

At this time, if the parent class has a constructor with parameters, the system will report an error.

When a constructor with two parameters is added to the parent class, the code will report an error.

Next, let me introduce the third method of using the super keyword:

super();

It can call the constructor of the parent class.

Note: It must be placed on the first line of the constructor.

Because there are two more important steps when instantiating an object :

  1. allocate memory for the object;
  2. Call the appropriate constructor.

The reason why the above code reported an error after we wrote the constructor with parameters is that the constructor of the subclass did not call the constructor of the parent class. (When we write the construction method ourselves, the system will no longer provide us with the construction method, and when the subclass object is constructed, we need to call the parent class construction method first, and then execute the subclass construction method. When we do not write the construction method method, the system will call the following construction method by default: , we only wrote the construction with parameters, so the system will report an error because it cannot find the construction without parameters, and we need to call it ourselves: )

The members in the subclass object are composed of two parts, the inherited part of the base class and the newly added part of the subclass. Father and son must have father first and then son, so when constructing a subclass object, first call the construction method of the base class to complete the construction of the members inherited from the base class, and then call the construction method of the subclass itself, which will The newly added members of the subclass are fully initialized.

Notice:

  1. If the parent class explicitly defines a no-argument or default construction method, there is an implicit super() call in the first line of the subclass construction method by default, that is, calling the base class construction method;
  2. If the parent class construction method has parameters, the user needs to explicitly define the construction method for the subclass, and select the appropriate parent class construction method call in the subclass construction method, otherwise the compilation will fail;
  3. In the subclass construction method, when super(...) calls the parent class construction, it must be the first statement in the subclass constructor;
  4. super(...) can only appear once in the subclass constructor, and cannot appear at the same time as this.
     

final

Only the following inheritance methods are supported in JAVA:

  • single inheritance;
  • multi-level inheritance;
  • Different classes inherit from the same class.

Generally, we don't want to have more than three levels of inheritance. If there are too many inheritance levels and too complicated, we need to consider refactoring the code.

So how should we stop others from letting them inherit?

JAVA provides a final key that can be used to modify variables, member methods, and classes.

  • Modified variables or fields, representing constants (that is, cannot be modified)
  • Modified class: Indicates that this class cannot be inherited
  • Modified method: Indicates that the method cannot be overridden

We just need to use final modification in front of the class that we don't want to be inherited:



 

Guess you like

Origin blog.csdn.net/2302_76339343/article/details/132042898