Details of java polymorphism and frequently asked questions

  • 2020-05-17 05:24:17
  • OfStack

java polymorphism

There are two types of polymorphism:

(1) compile-time polymorphism (design-time polymorphism) : method overload.

(2) runtime polymorphism: the JAVA runtime system decides which method to call based on the type of the instance calling the method, which is called runtime polymorphism. (we talk a lot about runtime polymorphism, so polymorphism is basically runtime polymorphism.)

There are three necessary conditions for the existence of polymorphism at runtime:

1. Inheritance (including interface implementation);
2. Rewrite;
3. The parent class reference points to the subclass object.

The benefits of polymorphism:

1. Substitutability (substitutability). Polymorphism is fungible for existing code. For example, polymorphic pairs of circles like Circle work, as do any other round geometry, such as a circle.

2. Extensibility (extensibility). Polymorphism is extensible to code. Adding new subclasses does not affect the polymorphism, inheritance, or operation of other features of existing classes. It's actually easier to subclass to get polymorphism. For example, it is easy to add the polymorphism of sphere class on the basis of realizing the polymorphism of cone, half cone and half sphere.

3. Interface (interface-ability). Polymorphism is achieved by the superclass providing a common interface to the subclass through method signature, which is perfected or overridden by the subclass. See figure 8.3. The superclass Shape in the figure specifies two interface methods that implement polymorphism, computeArea() and computeVolume(). Subclasses such as Circle and Sphere refine or override these two interface methods for polymorphism.

4. Flexibility (flexibility). It embodies flexible and diversified operation in the application and improves the use efficiency.

5. Simplification (simplicity). Polymorphism simplifies the coding 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).

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 is the output of the following?

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)); 1.
System. out. println (a1 show (c)); 2.
System. out. println (a1 show (d)); 3.
System. out. println (a2 show (b)); (4)
System. out. println (a2 show (c)); 5.
System. out. println (a2 show (d)); 6.
System. out. println (b show (b)); All landowners
System. out. println (b show (c)); today
System. out. println (b show (d)); Pet-name ruby

(3) answer

1) A and A
(2) A and A
(3) A and D
(4) B and A
(5) B and A
6 A and D
All landowners B and B
Today B and B
Pet-name ruby A and D

Analysis:

Use that order of priority at all times:

For question 1:

a1 is an instantiation object of A class, so this points to A, and then looks for this.show (b). Since there is no such method, super.show (b). So this.show (A), and you find this method in the A class, and you print A and A.

For question 2:

Similarly, a1 is an instantiated object of A class, so this points to A, and then looks for the this.show (C) method in A class. Since there is no such method, super.show (C). Because C's superclass is B, this.show (B) method was not found in A class, and B also has a superclass, A, so this.show (A) is found, and A and A is output.

For question 3:

Similarly, a1 is an instantiated object of A class, so this points to A, and then in A class, find this.show (D) method, find it, so output A and D;

For question 4:

a2 is a reference object of B class of type A, so this points to A class, and then looks for this.show (B) method in A class, but can't find it, so it goes to super.show (B). That is, super B = A, so execute the method this. show (A), find show (A) in A method, and find it, but since a2 is a reference object of class B, and B overwrites show (A) method of class A, the show (A) method of class B is finally executed, that is, output B and A;

For question 5:

a2 is the reference object of B class, and the type is A, so this points to A class, and then looks for this.show (C) method in A class, but it cannot be found, so it goes to super.show (C) method, because there is no superclass in A class, this. show(super C),C's superclass is B, so we looked for show (B) in A class, but again we didn't find it. We found that B also has a superclass, A, so we continued to look for show (A) method in A class. The B class covers the show (A) method of A class, so the show (A) method of B class is finally implemented, that is, output B and A;

For question 6:

a2 is the reference object of B class, A type, so this points to A class, and then looks for this. show(D) method in A class. So we're going to execute the show (D) method in the A class, which outputs A and D;

For question 7:

b is an instantiation object of B class. The prime minister implements this.show (B), looks for show (B) method in B class, and finds it.

For question 8:

b is an instantiation object of B class, the prime minister implements this.show (C), looks for show (C) method in B class, but does not find it, so he goes to super.show (c),B's superclass is A, so he looks for show (C) method in A class, but does not find it, this.show (super C),C's superclass is B, so look for show(B)f method in B class, and find it, so execute B and B B and B;

For question 9:

b is an instantiation object of B class.show(D) is implemented by the prime minister, and show (D) is found in B class.show(D) is not found, so show (D) is found in A class. Output A and D;

This is the method that I summarized after reading the topic on the Internet, I hope it is good for you.

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


Related articles: