Java Basics - Inheritance (ideas, overrides, super)

 

.1. What is inheritance? Why inherit?

Literally, inheritance is what a child inherits from his parents, or a skill or craftsmanship that an apprentice acquires from his master. Inheritance in java means that a subclass inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the instance domain and methods of the parent class, or the subclass inherits methods from the parent class, so that the subclass has the same behavior as the parent class.

Remember our DOTA heroes?

DOTA heroes can attack and release skills, so both Radiance and Dire heroes that inherit its characteristics can attack and release skills, but Radiance and Dire heroes can have their own unique attributes and behaviors (methods).

We have seen that both Radiance and Dire heroes have the attributes of name, age, race and belief, and have the behavior of attacking and releasing skills, but Radiance has a unique way of electing leaders, and Dire relies on strength superior,

Then we can use the extends keyword to write it as

Dota heroes are the parent classes of Radiant and Dire heroes, and Radiant and Dire heroes are subclasses of Dota heroes

Note: In java, only single inheritance is allowed, which means that a class can only explicitly inherit from one parent class at most. But a class can be inherited by multiple classes, which means that a class can have multiple subclasses. Simply put, a father can have multiple children, but a child can only have one father (the smile of the old king next door)

As can be seen from the above, the parent class stores the commonality, and the subclass stores the characteristics, so its role is

1. Solve the problem of code duplication and improve the maintainability and reusability of the code.

2. It is more about showing a system.

 In Java, except for the object class, each class has a direct parent class. The object class is the root class of the Java language, that is to say, the object is the ancestor.

 2. Method Override/Override

1. When calling a method through an object in the program, it will first check whether there is a corresponding method in the subclass. If the subclass exists, the method in the subclass will be executed. If the subclass does not exist, the parent class will be executed. in the corresponding method.

 

 When calling a method in a subclass, if the subclass does not have this method, you can directly access the method of the same name in the superclass.

When the same method as the parent class appears in the subclass, there will be an override operation, also known as override rewriting, overriding or overriding.

 

When the subclass needs the function of the parent class, and the function main subclass has its own unique content, it can rewrite the method in the parent class, so that it inherits the function of the parent class and defines the unique content of the subclass.

 2. The principle of method coverage

① The subclass method overrides the parent class method, and must ensure that the permission is greater than or equal to the parent class permission.

 Privately modified methods cannot be inherited by subclasses, so there is no concept of overriding

When the parent class method is greater than the access permission is greater than the subclass, a compilation error is reported

②The writing method must be exactly the same: the return value type method name parameter list of the method must be the same.

 Notice:

  1) Only the method has the concept of coverage, but the field does not. The method coverage is solved. A certain behavior of the parent class does not conform to the characteristics of the subclass. For example, fish is the parent class of whales. The breathing behavior of fish is to breathe with gills, while whales no

  2) For the package access permission member variable of the parent class, if the subclass and the parent class are under the same package, the subclass can inherit; otherwise, the subclass cannot inherit;

  3) For the parent class member variable that the subclass can inherit, if there is a member variable with the same name in the subclass, a hidden phenomenon will occur, that is, the member variable of the subclass will shield the member variable of the same name of the parent class. If you want to access the member variable of the same name in the parent class in the subclass, you need to use the super keyword to refer to it.

  4) For the package access permission member method of the parent class, if the subclass and the parent class are under the same package, the subclass can inherit; otherwise, the subclass cannot inherit;

  5) For the member method of the parent class that the subclass can inherit, if a member method with the same name appears in the subclass, it is called overriding, that is, the member method of the subclass will override the member method of the same name of the parent class. If you want to access the member method of the same name in the parent class in the subclass, you need to use the super keyword to refer to it.

3. super keyword

1. How do we call the method in the parent class after rewriting?

When we rewrite, use a method of the subclass to call the rewritten method of the superclass, the effect is as follows

In fact, the default in the skill() method in the subclass is this.atack();

 So what do we do?

Use the super keyword to access overridden methods in the parent class

 

Guess you like

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