Java zero foundation entry series – day13 inheritance and polymorphism of Java classes

Inheritance is a very important feature of a class. What? You don't even know about inheritance? Are you trying to piss dad off so you can inherit his inheritance? (funny)

Just kidding, inheritance here is very different from inheritance in our real life. One class can inherit another class. The inherited content includes attributes and methods. The inherited class is called the parent class or base class, and the inherited class is called the child class or export class. In the child class, the methods and variables of the parent class can be called. In Java, only single inheritance is allowed, that is, a class can only inherit from one parent class at most. However, a class can be inherited by multiple classes, that is, a class can have multiple subclasses. This is equivalent to a person can not have more than one father (funny, Lao Wang expressed dissatisfaction).

Without much to say, let's look at chestnuts first:

   Employee(String name, age,1)"> salary){.name =.age =.salary =

We define an employee class (Employee class), and define some simple member variables and methods. Next, we define a manager class (manager class) to inherit this class.

 Manager  Manager(String name,1)">super= 0 setBonus(.bonus = baseSalary =  baseSalary +

What needs to be explained here is the use of super and this. Super is the parent class reference, which can be used to call the methods and properties of the parent class. It can be regarded as a bridge between the parent class and its children, while this is its own reference, which can be used to call its own properties and methods. In the constructor, we use super (name, salary); This will call the constructor of the parent class,

Why can manager inherit the class employee? The reason is that there is an is-a relationship between them. The manager is also an employee. It has many of the same attributes as the employee, such as name, salary, and methods, such as name and salary, but it also has its own unique attributes and methods. It can also overload the methods of the parent class, such as getsalary in the above page. The manager class object here inherits the method of the parent class employee, so the manager object can directly use the getname () method and overload the getsalary method. Therefore, when calling the method of the manager object, it calls the getsalary method of the child class instead of the parent class,

What information can be inherited from the parent class?

  1. Subclasses can inherit member variables of the parent class

When a subclass inherits a class, it can use the member variables in the parent class, but it does not fully inherit all the member variables of the parent class. The specific principles are as follows:

1) be able to inherit the public and protected member variables of the parent class; Cannot inherit the private member variable of the parent class;

2) for the package access permission member variable of the parent class, if the child class and the parent class are under the same package, the child class can inherit; Otherwise, subclasses cannot inherit;

3) for the parent class member variable that can be inherited by the child class, if there is a member variable with the same name in the child class, it will be hidden, that is, the member variable of the child class will shield the member variable with the same name of the parent class. If you want to access a member variable with the same name in the parent class in a subclass, you need to use the super keyword for reference.

  2. The subclass inherits the methods of the parent class

Similarly, subclasses do not fully inherit all methods of the parent class.

1) be able to inherit the public and protected member methods of the parent class; Cannot inherit the private member method of the parent class;

2) for the package access permission member method of the parent class, if the child class and the parent class are under the same package, the child class can inherit; Otherwise, subclasses cannot inherit;

3) for the parent class member method that the subclass can inherit, if a member method with the same name appears in the subclass, it is called overwrite, that is, the member method of the subclass will overwrite the member method with the same name of the parent class. If you want to access a member method with the same name in the parent class in a subclass, you need to use the super keyword for reference.

It has been said many times about public, private and protected. There seems to be no formal introduction about access rights. Let's give a brief introduction by the way:

Java class has three access control characters: private, protected and public. At the same time, when these three access control characters are not written, they appear as a default access control state. Therefore, there are four levels of access control.

Specific access control performance is as follows:

The attributes or methods modified by private are unique to this class and cannot be accessed directly in any other class;

The attribute or method modified by default has package access characteristics, which can be accessed by other classes in the same package;

Protected modified attributes or methods can be accessed in other classes in the same package, and can also be accessed in subclasses not in the same package;

Public modified properties or methods can be accessed directly in external classes.

Why introduce the concept of access rights? Of course make complaints about packaging, just like making a machine, naturally want to hide all the wires in the box instead of swagger hanging outside, being tucking out, and it is safer. It's just for users or user programmers to see the contents that they want to show them, and all come swaggeringly hidden.

Although the subclass manager does not inherit the name and salary attributes of the parent employee, it does not mean that the operation on these two attributes is meaningless. It can be understood that a subclass object contains a parent object. For example, it is like we have assembled several different computers. For convenience, we can choose the same mainframe with the same power supply and fan configured, Other configurations can be different for each computer. Even if necessary, some computers can replace the fan and power supply. Although the final performance may be very different, they all look the same from the appearance. (of course, it's ok if you have to modify it completely.) the mainframe with fan and power supply configured here is equivalent to our parent class, and different computers are equivalent to subclasses. Subclasses can call the public methods of the parent class, such as turning the fan, but can't directly change the color of the mainframe, because the parent class doesn't provide such permission. But this doesn't mean that the host class The color of the chassis is of no use to the subclass. It is still part of the subclass, but it can't be operated directly.

That's all for the access permission. Now let's return to our inheritance. Here's a chestnut for using the manager class:

 =  Manager("Frank",10000010000=  Employee[30] =1] =  Employee("Alan",80002] =  Employee("Tom",9000"name:"+e.getName()+" salary:"+

Here, we define an employee array, and then assign a manager variable to the first element of the employee array. Seeing here, you may be confused. Doesn't it mean that assignment can only be used between variables of the same type? That's true, but because the manager class is a subclass of employee, a manager object has all the attributes and methods of employee at the same time, that is, it can do the same thing that employee can do. Therefore, it's no problem to assign the variables of the manager class to the employee variable, but not vice versa, because the manager class has its own method setbonus(), Employee cannot be implemented. When traversing the output, we use all elements as employee objects. The output is as follows:

name:Frank salary:110000.0name:Alan salary:8000.0name:Tom salary:9000.0

When we call the getsalary method of the boss variable, we obviously call the method of the subclass and return it after adding the basic salary to the bonus.

That said so much, why do you have to use inheritance?

The reason is very simple. One is that the code can be reused. Like this example, the getname method of employee is reused by the subclass manager. This method can be used directly in the manager, which can save a lot of code.

Secondly, polymorphism can be realized. You may not believe it. The chestnut we just used a great concept - polymorphism. When traversing the output, the reference of a parent object points to the child object and calls the child method.

So what are the benefits of doing so? What is the significance of polymorphism?

Simple and convenient. Continue to use the chestnut we just mentioned. If we now have a personnel management class, personnelmanagement, which needs to enter employee information and a record method, if we use the polymorphic feature, we only need to pass in an employee object to the record method, Both managers and ordinary employees can handle it in the same way, otherwise we need to design a method for managers and employees respectively, which may be fine, but if there are many other positions, such as general manager, deputy manager, manager assistant, human resources manager and purchasing manager, can you design a method for each position? Obviously unrealistic, and this will lose scalability and flexibility, and turn an artistic work into manual work, which will make you lose the fun of programming.

Therefore, inheritance and polymorphism are also very simple. Inheritance is to use extensions to inherit the properties and methods of the parent class. Polymorphism can treat the subclass object as the parent object for unified processing at the appropriate time, so as to realize and increase the reuse of the code and make your code more and more attractive.

So far, the explanation of class inheritance and polymorphism is finished. Welcome to continue your attention! If you like my tutorial, remember to use your little hand to click on the recommendation. You are also welcome to pay attention to my blog.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>