This is the fifth day of my participation in Gwen Challenge

Class inheritance

With that in mind, let’s consider a question. If two classes have identical variables and methods, and only one class has its own unique variables and methods, do we really need to write the same variables and methods in both classes?

For example, if a class Student represents a Student, it only has a score member variable relative to class Person. Do you need to define the name field as follows?

/** ** student **@authorThe snail *@fromPublic number: snail Internet */
public class Student {

    /** * name */
    String name;

    /** * score */
    int score;

}
Copy the code

This obviously leads to code reuse issues! Can we not write duplicate code in Student?

This is where inheritance in Java comes in handy. Inheritance is a powerful mechanism for object-oriented programming that allows subclasses to inherit the characteristics and behavior of their parent class, and to allow subclass objects to have instance variables and methods of their parent class.

A child class inherits from a parent class, and a parent class derives from a child class. A parent class is also called a base class and a subclass is also called a derived class.

Generally speaking, the hierarchy of classes is always more specific at the lower level than at the upper level, and contains the characteristics of the upper level so that the lower class automatically shares the characteristics and properties of the upper class. Inheritance is the mechanism by which derived classes automatically share member variables and member methods in the base class.

In Java, inheritance is implemented through the extends keyword, and all classes inherit from java.lang.Object, so this is a true representation of everything objects in Java. Why, you might wonder, can a custom class inherit Object without the extends keyword? This is because this class is in the java.lang package and Java already supports it by default.

package cn.java4u.oo;

/** ** student **@authorThe snail *@fromPublic number: snail Internet */
public class Student extends Person {

    /** * score */
    int score;

}
Copy the code

Now that we know the basics of inheritance, what does inheritance do?

First, inheritance is a powerful tool for automatically propagating and reusing code. It can extend new classes to existing classes, reducing the duplication of code, but also because of the reduction of redundancy, consistency is enhanced, thus improving the maintainability of the program.

Secondly, inheritance can clearly reflect the hierarchical relationship between classes, which improves the readability of code.

In addition, inheritance is unidirectional, meaning that derived classes can inherit and access base class members, but not the other way around. And unlike C++, Java allows only single inheritance, meaning that a derived class cannot inherit from more than one base class.

When using inheritance, consider the access control rights of the base class members. Refer to the access control description that encapsulates that piece of content.

The process of subclass instantiation

In particular, the constructor of a parent class cannot be inherited by subclasses, even if it is public. The constructor of a parent class initializes its own member variables, while the constructor of a child class takes care of its own unique member variables, not the state of the parent class.

package cn.java4u.oo.inherit;

/** * defines the parent class **@authorThe snail *@fromPublic number: snail Internet */
public class Parent {


    /** * constructor */
    public Parent(a) {

        System.out.println("This is the constructor of the Parent class."); }}package cn.java4u.oo.inherit;

/** * define subclass **@authorThe snail *@fromPublic number: snail Internet */
public class Child extends Parent {

    /** * constructor */
    public Child(a) {

        System.out.println("This is the constructor for subclass Child."); }}package cn.java4u.test;

import cn.java4u.oo.inherit.Child;

/ * * *@authorThe snail *@fromPublic number: snail Internet */
public class InheritTest {

    public static void main(String[] args) {

        Child child = newChild(); }}Copy the code

Therefore, when instantiating an object of a subclass, Java executes the constructor of the parent class first, and then the constructor of the subclass. If the parent class has a higher parent class, the constructor of the higher parent class is called first, and all the parent constructors of the inherited relationship are executed one by one. If the constructor of the parent class fails, the object of the subclass cannot be instantiated.

When the above code runs, it will print:

This is the constructor for Parent and this is the constructor for ChildCopy the code

With this super

If calling the parent constructor involves a parameter constructor, you can use the super keyword to call the parent constructor and pass the parameters.

Super also has the ability to access a member of a parent class and a member of a subclass if they have the same name, the subclass can only access its own member by default, so if you want to access a member of the parent class, you can use super. Syntax implementation of member names. But this is only possible if the member of the parent class cannot be private.

The keyword opposed to super is this, which refers to the parent class of the current object, and this refers to the current object itself. This is often used to distinguish member variables from local variables, as in the following code, where I add a parameter constructor.

public class Parent {

    int a;

    /** * constructor */
    public Parent(a) {

        System.out.println("This is the constructor of the Parent class.");
    }

    public Parent(int a) {
        this.a = a; }}Copy the code