Java Object-Oriented-Inheritance Analysis

inherit:

    It is to build a new class on the basis of the existing class. The new class is called a subclass of the existing class. The subclass will automatically have all the non-private attributes and behaviors of the parent class, and can also extend its own unique functions. .

Inherited features:

    1. Only single inheritance is supported in java, multiple inheritance is not supported

    2. Support multi-layer inheritance (form inheritance system)

Issues to be aware of in inheritance:

    1. A subclass can only inherit non-private members of the parent class (member variables and member methods)

    2. The constructor of the parent class can not be inherited by subclasses, but the constructor of the parent class can be called through the super keyword

    3. Inheritance reflects an "is a" relationship (for example, Persian cats are a type of cat, and cats are a type of animals). Don't deliberately inherit some functions.

class A{

    public void a() {

    }

    public void b() {

    }

}

/*class B {

    public void b() {

    }

    public void c() {

    }

}*/

class B extends A {

    public void c() {

    }

}

Just like when you find that the same method b as in class A appears in class B, if you use inheritance, you will find that method a will also be inherited, but a may not be the function you want.

The relationship of member variables in inheritance:

    1. Member variables with different names can be accessed normally

    2. The member variables have the same name (the principle of proximity), and the search order of the subclass is: subclass local scope -> subclass member scope -> parent class member scope

The relationship of member methods in inheritance:

    1. Member method declarations are different, normal access

    2. The member method declaration is the same (the method is rewritten), the subclass search order: find the subclass -> find the parent class

The relationship of constructors in inheritance:

    1. Constructors cannot be inherited and can be accessed through the super keyword

    2. The subclass will access the empty parameter structure of the parent class by default. The reason is that the initialization of the subclass may use the data of the parent class. Before the initialization of the subclass, the initialization of the data of the parent class must be completed. Statements are all super();.

Method overriding in inheritance:

    Method override (override): The same method declared in the parent class appears in the subclass.

    Application of method rewriting: When the subclass needs the functions of the parent class, and the subclass itself has its own unique functions, it needs to rewrite the functions of the parent class, so that it not only has the functions of the parent class, but also maintains its own unique functions. function.

    Notes on method overriding:

        1. The subclass overrides the parent class method, and the access permission should be greater than or equal to the parent class permission (preferably consistent)

        2. When overriding, the method declaration should be exactly the same as the parent class

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324839979&siteId=291194637