Understanding of Polymorphism in Java

Understanding polymorphism in Java Polymorphism
in Java can be simply understood as multiple forms of a thing. Of course, polymorphism exists only when there is rewriting on the basis of inheritance. (Because this article is reproduced, most places support it, but there are also some places where personal attitudes exist. For example, here, I think interface implementation can also be regarded as polymorphism).

How to understand the multiple forms of a thing?
For example, take human beings, men, women. Men, women, have inherited human beings respectively!
Humans have noses, eyes, and mouths, whether male or female.
Humans eat, sleep, and walk, man or woman.
But both men and women have some skills that are unique to them. For example, most men are very strong. Most women are beautiful.
The above is a premise. Let's do it with code.
The following is the definition of the People class
insert image description here

The following is the definition of the male class: and override the eat and walk methods of People, and the man also has his own unique method as power
insert image description here

The female class is defined below: the female class also rewrites People's eat and walk methods, and women have their own unique methods beautiful
insert image description here

When we declare the People class in the Main method: the people object can call the declared methods and properties in the People class, this does not need to be explained. As shown below:
insert image description here

We can use to create a reference to a parent class and use an object of a subclass to assign a value.
To put it bluntly, creating a parent class reference is to construct an object of the parent class. In our current logic, it is to create a People object; using
a subclass object to assign a value means that the subclass object can be used to replace the previous new People ( );
insert image description here

Because both the Man class and the Woman class inherit the People class, that is to say, when People are used, I can directly replace them with the subclasses of People. It is also possible to declare Woman in the same way:
insert image description here

This way of declaring has a characteristic, that is, how many methods the created object p1 can call, you can see the class on the left, in People p1 = new Woman();, how many properties and methods p1 can call, you can see the People on the left kind. As shown below:
insert image description here

Some properties and methods of the parent class can be called. When we call the eat() method, run:
insert image description here

insert image description here

We found that the eat method in Woman is running.
Let's change the name of the eat method in Woman from eat to eatsss
insert image description here

Re-executing p1.eat()
runs the method in the parent class:
insert image description here

Add an attribute int ID to the People class and assign a value of 1;
insert image description here

Add an attribute int ID = 2 to the Woman class;
insert image description here

Call p1.ID in the main function and print
insert image description here

The result is 1
insert image description here

That is to say, in this case: People p1 = new Woman();
**The attribute to be called looks at the attribute on the left, and the method needs to be checked to see if it is rewritten, because you can only call out the attributes and methods in People , if there is an overridden method with the same name in the class on the right, it will be called, if there is no overridden method, the method in the class on the left will be called

Or use the above three classes to describe:
In the class where the main method is located, add a method one by one:
insert image description here

It is a People class. As we said above, wherever the parent class appears, we can replace it with a subclass.
When we call this method:
insert image description here

This is the use in polymorphism. The specific method that the incoming object calls depends on whether there is rewriting. If there is rewriting, it will be rewritten by subclasses, and if it is not rewritten, it will be rewritten by the parent class.

Note: This article is a reprinted article, which means that the blogger agrees with the blogger's point of view from which the article originated. I hope that you will give me advice and improve the inappropriateness of the article, so as not to mislead the children, thank you!

Reprinted in: https://blog.csdn.net/zhangguan96/article/details/123721254

Guess you like

Origin blog.csdn.net/qq_41774102/article/details/127107315