Article From:https://www.cnblogs.com/leonardo-Xx/p/9216566.html

To sum up in a word, there are different states in the course of running things. Let me give you an example of a textbook. Let me give you an example of Mulan joining the army for his father. There are three prerequisites for the existence of polymorphism.
1.Have an inheritance
2.A subclass to rewrite the method of the parent class
3.The parent class reference points to the subclass pair,
But there are a lot of details to be paid attention to. First we define two classes, a parent Animal and a subclass Cat.
Parent class Animal

class Animal {
	int num = 10;
	static int age = 20;
	public void eat() {
		System.out.println("Animals eat ");
	}

	public static void sleep() {
		System.out.println("Animals are sleeping ");
	}

	public void run(){
		System.out.println("Animals are running "running");
	}
}

Subclass Cat

class Cat extends Animal {
	int num = 80;
	static int age = 90;
        String name = "tomCat";
	public void eat() {
		System.out.println("A cat eats.);
	}
	public static void sleep() {
		System.out.println("The cat is sleeping ");
	}
	public void catchMouse() {
		System.out.println("The cat is catching the mouse. ");
	}

}

Test class Demo_Test1

class Demo_Test1 {
	public static void main(String[] args) {	
	Animal am = new Cat();
	am.eat();
	am.sleep();
	am.run();
	//am.catchMouse();This will be annotated first, and so on.//System.out.println(am.name);//Annotated first here, to be explainedSystem.out.println(am.num);
	System.out.println(am.age);
	}
}

The above three codes fully embody the three premises of polymorphism:
1、Existence of inheritance
CatClasses inherit the Animal class
2、A subclass to rewrite the method of the parent class
The subclass rewrites (override) the two member method of the parent class eat (), sleep (). Where eat () is not static, sleep () is static (static).
3、The reference of the parent class data type points to the subclass object.
The Animal am = new Cat () in the test class Demo_Test1; the statement opens the object of the subclass (Cat) in the heap memory and points the reference of the parent class (Animal) in the stack memory to the Cat object.
Thus, the necessary three prerequisites for Java polymorphism are satisfied.
—————————————————Splendid segmentation line.
If we look at it further, we can look at the output of the test class above, and perhaps have a deeper understanding of polymorphism. Guess what the result is.
It can be seen
The subclass Cat overrides the non static member method am.eat () of the parent class Animal; the output result is: the cat eats.
The subclass rewrites the static member method am.sleep () of the parent class (Animal); the output result is: the animal is asleep.
The parent class (Animal) method am.run () that is not rewritten by the child class (Cat) outputs the result: the animal is running.

System.out.println(am.num);//The output result is 10System.out.println(am.age);//The output result is 20

We can summarize the characteristics of polymorphic members based on the above situation:
Member variable
Compile and look on the left (parent class), run to the left (parent class)
Member method
Compile the left side (parent class) and run to the right (Zi Lei). Dynamic binding
Static method
Compile the left side (parent class) and run to the left (parent class).
(Static and class related are not rewritten, so access is still left.
Only the non static member method is compiled, look left, run to see the right side.
—————————————————Splendid segmentation line.
So what’s the malpractice of polymorphism? Yes, there are no specific attributes and methods for subclasses after polymorphism. To see the code above, subclass Cat has a unique attribute String name = “tomCat”; and there is a unique way to catch mice, catchMo.Use (). But in the test class (Demo_Test), we try to call the subclass specific method catchMouse () and the signature of the printer class, String name = “tomCat”; it will be wrong.

am.catchMouse();
System.out.println(am.name);

The reason is that the disadvantage of polymorphism is that we cannot use the unique member attributes of subclasses and the unique member methods of subclasses.
————————————————–Splendid segmentation line.
What if you want to use the specific attribute String name in the Cat class and its unique member method catchMouse ()? Then we can refer the parent class to the am of the subclass object and then force it back to the Cat type. suchAm is the reference of Cat type. It also refers to Cat object. It can also use all attributes of Cat class and all member methods.

class Demo_Test {
	public static void main(String[] args) {
		
	Animal am = new Cat();
	am.eat();
	am.sleep();
	am.run();
//	am.catchMouse();
//	System.out.println(am.name);
	System.out.println(am.num);
	System.out.println(am.age);
	
	System.out.println("------------------------------");
	Cat ct = (Cat)am;
	ct.eat();
	ct.sleep();
	ct.run();
	ct.catchMouse();
	}        
}

It is obvious that the execution of Cat CT = (Cat) am is strong; after that, CT points to the object of the Cat type originally created in heap memory. This is the charm of polymorphism. Although it has shortcomings, it is very flexible to reduce the creation of redundant objects, needless to say.A subclass method is used to re create a new subclass object in the heap memory. Above..
———————————————————-Dividing line
La La, received so much praise. Surprised, let’s take a popular example.
Mulan went to the army for his father
Everyone knows that Mulan joined the army for his father. Mulan joined the army for his father. At this time, Mulan is a subclass and flower arc is the parent class. Flower arc has its own member attribute age, name, gender. Mulan also has these attributes, but it is obvious that the attributes of the two are totally different. The flower arc has its own non static formationThe method of “riding a horse to kill an enemy” means that Mulan also inherited his father’s method of “riding a horse to kill an enemy”. The flower arc has a static way of introducing itself, and everyone can ask the flower arc name. At the same time, Mulan also has his own unique non static method of “painting up”. But now, MulanBe a man for a man. At this time, the reference of the parent class (the name of the flower arc) points to the subclass object (Mulan), so when the other classes (other people) have access to the subclass objects (name, age, sex) of the subclass object (the person of Mulan), they are all Mulan her.Father’s name (flower arc), age (60 years old), gender (male). When visiting the subclass object (Mulan) the non – static member method (riding on a horse), he actually sees Mulan himself using eighteen Wu Yi to fight on horseback. When visiting Mulan’s static method (self introduction), flowers and treesLAN himself used her father’s name to introduce himself to others. At the same time, Mulan could not use his unique member method to “paint”. – —- upwards transition in polymorphism
At last, the war was successful, and Mulan said goodbye to war life. One day, when he met his beloved man, the power of love forced the reference of the parent object (the name of the flower arc) to the original reference of the subclass object (the name of Mulan), then Mulan againFrom the new to herself, this time she was completely herself. The name is Mulan, the age is 28, the sex is female, and the war is still so fierce. I introduce myself to Mulan. OMG! Finally, you can finally use your unique member method to “paint”.. From then on, Mulan returned to Mulan who had joined the army for his father. And spend the rest of his life happily with his beloved man. – —- the downward transformation of polymorphism
———————————————————-Splendid segmentation line.
You remember ha, the transition to the upwards transition must be under the premise of polymorphism, otherwise the compulsory to turn the daughter into a father, or to turn the father into a woman, will become undefeated in the East, and the system will report the wrong type of conversion at this time. Ha ha ha ha ha ha ha. In addition, the development of the general use of polymorphic declaration form parameters, and willThe anonymous object of the subclass is created as the actual parameter. Above.

Leave a Reply

Your email address will not be published. Required fields are marked *