A detailed explanation of Java polymorphism and common face test _java

Source: Internet
Author: User
Tags inheritance

Java polymorphism

Polymorphism is divided into two types:

(1) Compile-time polymorphic (design-time Polymorphism): Method overload.

(2) Run-time polymorphism: The Java runtime system determines which method is called Run-time polymorphism based on the type of the instance that invokes the method. (we usually speak much of the things in the Run-time polymorphism, so polymorphic mainly refers to the run-time polymorphism)

Three necessary conditions for Run-time polymorphism:

First, must have the inheritance (including the interface realization);
Second, to have to rewrite;
Third, the parent class reference points to the child class object.

The benefits of polymorphism:

1. interchangeability (substitutability). polymorphic pairs of existing code are replaceable. For example, polymorphic pairs of circular circle work on any other circular geometry, such as rings, also work.

2. Extensibility (Extensibility). Polymorphic is extensible for code. Adding new subclasses does not affect the polymorphism of existing classes, inheritance, and the operation and operation of other features. In fact, the new subclass is more likely to get polymorphic functionality. For example, in the realization of the cone, half cone and hemispherical body on the basis of polymorphism, it is easy to add the sphere of the polymorphism of the class.

3. Interface (interface-ability). Polymorphism is a superclass that is implemented by means of a method signature that provides a common interface to subclasses to refine or overwrite. As shown in Figure 8.3. The superclass shape in the figure prescribes two interface methods for implementing polymorphism, Computearea () and Computevolume (). Subclasses, such as circle and sphere, to implement polymorphism, perfect or overwrite these two interface methods.

4. Flexibility (flexibility). It embodies the flexible and various operation in the application, and improves the usage efficiency.

5. Simplification (simplicity). Polymorphism simplifies the process of coding and modifying the code of application software, especially in dealing with the operation and operation of a large number of objects, which is especially important.

Note: precedence 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 ... {}

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

(c) The 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 the first question:

A1 is an instantiated object of Class A, so this point to a and then look for This.show (b), because there is no such method, so to Super.show (b), but because Class A does not have a superclass, so to This.show (Super B), because the superclass of B is a, So it's equivalent to This.show (a), and then find this method in Class A, and output a and a.

For the second question:

Similarly, A1 is an instantiated object of Class A, so this points to a and then looks up the This.show (c) method in Class A, because there is no such method, so to Super.show (c), because the superclass of Class A is looking for it, but a has no superclass, so to This.show ( Super C), because the superclass of C is B so look up the This.show (b) method in class A, also not found, then B also have superclass, is a, so look for This.show (a), found, and then output A and A;

For the third question:

Similarly, A1 is an instantiated object of Class A, so this points to a, then finds the This.show (d) method in Class A, and finds it, so it outputs a and D;

For question fourth:

A2 is a reference object of class B, the type is a, so this points to class A and then finds the This.show (b) method in Class A, not found, so Super.show (b), because Class A does not have a superclass, so to This.show (Super B), B's Super class is a , that is, super B = A, so Execute method this. Show (a), find show in a method (a), yes, but since A2 is a reference object of Class B, and Class B covers the show (a) method of Class A, it ultimately executes the show (a) method inside Class B, which outputs B and A;

For question fifth:

A2 is a reference object of Class B, the type A, so this points to class A, and then finds the This.show (c) method in Class A, which is not found, so the Super.show (c) method, because Class A does not have a superclass, so to This.show (Super C), C's Super class is B, so find show in class A (b), also did not find, found B also has a superclass, that is, a, so also continue to find show in a class (a) method, found, but because A2 is a reference object of Class B, and B class inside covered a class of show (a) method, So the final execution is the show (a) method in Class B, which outputs B and A;

For question sixth:

A2 is a reference object of Class B, the type A, so this points to class A and then finds the This.show (d) method inside a class, but because A2 is a reference object of Class B, find out if there are any overlay show (d) Methods in Class B, no, So the show (d) method in Class A is executed, which outputs a and D;

For question seventh:

B is an instantiated object of Class B, the Prime Minister executes This.show (b), find Show (b) in class B, find, direct output B and B;

For question eighth:

B is an instantiated object of Class B, the Prime Minister executes This.show (c) and finds Show (c) in Class B, which is not found, so to Super.show (c), B's Super class is a, so find show in Class A (c) method, not found, So to This.show (Super C), C's Super class is B, so in Class B, find show (b) F method, found, so execute show (b) method in class B to output B and B;

For question Nineth:

B is an instantiated object of Class B, the Prime Minister executes This.show (d) and finds show (d) in Class B, which is not found, and then to Super.show (d), B's Superclass is Class A, so find show (d) in Class A, find, output A and D;

This is the way I have read the topic of the Internet, summed up the method, I hope to be good for everyone.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.