Detailed explanation of Java polymorphism and common interview questions

Java polymorphism

There are two types of polymorphism:

(1) Compile time polymorphism (design time polymorphism): method overloading.

(2) Runtime polymorphism: the Java runtime system determines which method to call according to the type of the instance calling the method, which is called runtime polymorphism. (we usually talk a lot about runtime polymorphism, so polymorphism mainly refers to runtime polymorphism)

Three necessary conditions for the existence of runtime polymorphism:

1、 There should be inheritance (including interface implementation); 2. There should be rewriting; 3. The parent class reference points to the child class object.

Benefits of polymorphism:

1. Substitutability. Polymorphic pairs are replaceable for existing code. For example, polymorphic pairs work for the circle class, as do any other circular geometry, such as a ring.

2. Extensibility. Polymorphism is extensible to code. Adding new subclasses does not affect the polymorphism, inheritance, and operation of other features of existing classes. In fact, it is easier for new subclasses to obtain polymorphism functions. For example, it is easy to add the polymorphism of sphere class on the basis of realizing the polymorphism of cone, semi cone and hemisphere.

3. Interface capability. Polymorphism refers to that a superclass provides a common interface to a subclass through method signature, and the subclass improves or overwrites it, as shown in Figure 8.3. The superclass shape in the figure specifies two interface methods that implement polymorphism, computerarea() and computevolume(). Subclasses, such as circle and sphere, improve or cover these two interface methods in order to achieve polymorphism.

4. Flexibility. It embodies flexible and diverse operations in the application and improves the use efficiency.

5. Simplicity. Polymorphism simplifies the code writing and modification process of application software, especially when dealing with the operation and operation of a large number of objects.

Note: priority from high to low: this show(O)、super. show(O)、this. show((super)O)、super. show((super)O)。

Relevant interview questions:

(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)); ⑨

(3) 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

analysis:

To do this, always use that priority order:

For question 1:

A1 is an instantiated object of class A, so this points to a, and then find this Show (b). Since there is no such method, go to super Show (b), but since class A has no superclass, go to this. Show (Super B). Since the superclass of B is a, it is equivalent to this Show (a), then find this method in class A, and output a and a.

For question 2:

Similarly, A1 is an instantiated object of class A, so this points to a, and then find this in class A Show (c) method. Because there is no such method, it goes to super. Show (c). Because it is found in the superclass of class A, but a has no superclass, it goes to this Show (super C). Since the super class of C is B, find this in class A The show (b) method is not found, and B also has a superclass, that is, a, so find this Show (a), find it, and 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 in class A The show (d) method is found, so it outputs a and D;

For question 4:

A2 is the reference object of class B, type A, so this points to class A, and then find this in class A The show (b) method was not found, so it reached super Show (b). Class A has no superclass, so this Show (Super B). B's superclass is a, that is, Super B = a, so it executes the method this. Show (a). Find show (a) in method a and find it. However, since A2 is a reference object of class B and class B covers the show (a) method of class A, it finally executes the show (a) method in class B, that is, outputs B and a;

For question 5:

A2 is the reference object of class B, type A, so this points to class A, and then find this in class A The show (c) method was not found, so it reached super Show (c) method. Since class A has no superclass, when you get to this. Show (super C), the superclass of C is B, Therefore, show (b) was found in class A, but it was also not found. It was found that B has a superclass, that is, A. therefore, we continued to find the show (a) method in class A, and found it. However, since A2 is a reference object of class B, and class B covers the show (a) method of class A, we finally executed the show (a) method in class B, that is, output B and a;

For question 6:

A2 is the reference object of class B, type A, so this points to class A, and then find this in class A The show (d) method is found. However, since A2 is a reference object of class B, find whether the show (d) method is overwritten in class B. No, so it executes the show (d) method in class A, that is, outputs a and D;

For question 7:

B is an instantiated object of class B, which executes this Show (b). Find the show (b) method in class B. if it is found, directly output B and B;

For question 8:

B is an instantiated object of class B, which executes this Show (c) finds the show (c) method in class B, but it is not found, so it goes to super.show (c). B's superclass is a, so it finds the show (c) method in class A, but it is not found, so it goes to this.show (super C). C's superclass is B, so it finds the show (b) f method in class B, so it executes the show (b) method in class B and outputs B and B;

For question 9:

B is an instantiated object of class B, which executes this Show (d), find the show (d) method in class B, but it is not found, so go to super.show (d). The superclass of B is class A, so find the show (d) method in class A, find it, and output a and D;

This is the method I summarized after reading the online topics. I hope it will be beneficial to you.

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>