Java polymorphism
There are two types of polymorphism:
(1) Compilation-time polymorphism (design-time polymorphism): method overloading.
(2) Runtime polymorphism: The JAVA runtime system decides which method to call based on the type of instance invoking the method, which is called runtime polymorphism. (The things we usually talk about are runtime polymorphism, so polymorphism mainly refers to runtime polymorphism)
Three necessary conditions for the existence of polymorphism at runtime:
1. There must be inheritance (including the implementation of interfaces);
2. There must be rewrite;
3. The parent class reference points to the child class object.
Benefits of polymorphism:
1. Substitutability. Polymorphisms are interchangeable for existing codes. For example, polymorphism works on the circle Circle class, and also works on any other circular geometry, such as a circle.
2. Extensibility. Polymorphism is scalable to code. Adding new subclasses does not affect the operation and operation of existing classes' polymorphisms, inheritance, and other characteristics. In fact, it is easier to obtain polymorphic functions by adding new subclasses. For example, on the basis of realizing the polymorphism of cones, semicones and hemispheres, it is easy to add polymorphisms of spheres.
3. Interface-ability. Polymorphism is achieved by superclasses through method signatures, providing a common interface to subclasses, and subclasses are implemented by subclasses to improve or overwrite it. As shown in Figure 8.3. In the figure, the super class Shape specifies two interface methods that implement polymorphism, computeArea() and computeVolume(). Subclasses, such as Circle and Sphere, improve or override these two interface methods in order to achieve polymorphism.
4. Flexibility. It reflects flexible and diverse operations in applications and improves usage efficiency.
5. Simplicity. Polymorphism simplifies the code writing and modification process of application software, especially when dealing with the operations and operations of a large number of objects, this feature is particularly prominent and important.
Note: Priority is from high to low: this.show(O), super.show(O), this.show((super)O), super.show((super)O).
Related interview questions:
class A { public String show(D obj)..{ return ("A and D"); } public String show(A obj)..{ return ("A and A"); } } class B extends A{ public String show(B obj)..{ return ("B and B"); } public String show(A obj)..{ return ("B and A"); } } class C extends B...{} class D extends B...{}(2) Question: What are the following output results?
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println(a1.show(b)); ①
System.out.println(a1.show(c)); ②
System.out.println(a1.show(d)); ③
System.out.println(a2.show(b)); ④
System.out.println(a2.show(c)); ⑤
System.out.println(a2.show(d)); ⑥
System.out.println(b.show(b)); ⑦
System.out.println(b.show(c)); ⑧
System.out.println(b.show(d)); ⑨
(III) Answer
① A and A
② A and A
③ A and D
④ B and A
⑤ B and A
⑥ A and D
⑦ B and B
⑧ B and B
⑨ A and D
analyze:
When doing this kind of question, you must always use the priority order:
For question one:
a1 is an instantiated object of class A, so this points to A, and then looks for this.show(b). Since there is no method, it goes to super.show(b). However, since class A has no superclass, it goes to this.show(super b). Since b's superclass is A, it is equivalent to this.show(A). Then, this method is found in class A, so A and A are output.
For question 2:
Similarly, a1 is an instantiated object of Class A, so this points to A, and then search for this.show(C) method in Class A. Since there is no such method, I go to super.show(C). Since I look for this in the superclass of Class A, but A does not have a superclass, so I go to this.show(super C). Since C's superclass is B, I look for this.show(B) method in Class A, and I didn't find it. Then B also has a superclass, which is A, so I look for this.show(A), and I found it, so I output A and A;
For question 3:
Similarly, a1 is an instantiated object of class A, so this points to A, and then find this.show(D) method in class A, and it is found, so A and D is output;
For question 4:
a2 is a reference object of class B, with type A, so this points to class A, and then find this.show(B) method in class A, but it is not found, so it reaches super.show(B). Since class A does not have a superclass, it arrives this.show(super B). The superclass of B is A, that is, super B = A, so execute this method. show(A), look for show(A) in method A, and find it, but since a2 is a reference object of class B, and class B covers the show(A) method of class A, the final execution of the show(A) method in class B, that is, output B and A;
For question 5:
a2 is a reference object of class B, with type A, so this points to Class A, and then looks for this.show(C) method in Class A, but it was not found, so it was the super.show(C) method. Since Class A does not have a superclass, it is B, so it is B, so it is found in Class A, but it is not found. B also has a superclass, that is, A, so it continues to look for the show (A) method in Class A, and found, but because a2 is a reference object of class B, and Class B covers the show (A) method of class A, so it is finally executed. The show (A) method in Class B, that is, the output of B and A;
For question 6:
a2 is a reference object of class B, with type A, so this points to class A, and then find this.show(D) method in class A, and found it. However, since a2 is a reference object of class B, it is found in class B, so it is found whether there is any overridden show (D) method in class B, so it is executed. So the show (D) method in class A, that is, output A and D;
For question seven:
b is an instantiated object of Class B. The Prime Minister executes this.show(B), looks for the show(B) method in Class B, finds it, and directly outputs B and B;
For question 8:
b is an instantiated object of class B. The Prime Minister executes this.show(C), and looks for the show(C) method in class B, but it is not found, so when I go to super.show(c), the superclass of B is A, so when I look for the show(C) method in class A, but it is not found, so when I go to this.show(super C), the superclass of C is B, so when I look for the show(B)f method in class B, I find it, so when I execute the show(B) method in class B, I output B and B;
For question 9:
b is an instantiated object of Class B. The Prime Minister executes this.show(D), looks for the show(D) method in Class B, but it is not found, so I went to super.show(D), and the superclass of B is Class A, so I look for the show(D) method in Class A, and I found it, and output A and D;
This is the method I have summarized after reading the questions on the Internet. I hope it will be beneficial to everyone.
Thank you for reading, I hope it can help you. Thank you for your support for this site!