Do you really understand inheritance and polymorphism?

Source: Internet
Author: User
Recently, my colleague asked a question about scjp, which is related to inheritance and polymorphism. I will not repeat the specific questions. Let's take a look at my own questions. Code :

1 Package Sean. Work. test;
2
3 Public   Class Doyoureallyunderstandpolymorphism {
4
5 Public   Static   Void Main (string [] ARGs) {
6 A =   New A ();
7 B =   New B ();
8 A.s =   " [AA] " ;
9 B. S =   " [BB] " ;
10 System. Out . Println (a. s ); // Prints "[AA]"
11 System. Out . Println (B. S ); // Prints "[BB]"
12 System. Out . Println (A. Gets ()); // Prints "[AA]"
13 System. Out . Println (B. Gets ()); // Prints "[BB]"
14 System. Out . Println ( " ================================ " );
15 A = B; // A now refers to object B
16 System. Out . Println (a. s ); // Prints "[a]" <-- 1 -- the Class A copy
17 System. Out . Println (B. S ); // Prints "[BB]"
18 System. Out . Println (A. Gets ()); // Prints "[BB]"
19 System. Out . Println (B. Gets ()); // Prints "[BB]"
20 System. Out . Println ( " ================================ " );
21 (A) B). S =   " [AA] " ; // <-- 2 -- changes the Class A copy in object B
22 System. Out . Println (a. s ); // Prints "[AA]" <-- 3 -- class A copy changed
23 System. Out . Println (B. S ); // Prints "[BB]"
24 System. Out . Println (A. Gets ()); // Prints "[BB]"
25 System. Out . Println (B. Gets ()); // Prints "[BB]"
26 }

27
28 }

29
30 Class A {
31 String s =   " [A] " ;
32 String gets () {
33ReturnS;
34}

35 }

36
37 Class B extends {
38 String s =   " [B] " ;
39 String gets () {
40ReturnS;
41}

42 }

43

Here, Class B inherits from Class A and overwrites the gets () method, so we can use polymorphism. If you pay attention to the lines 15, 16, 21, and 22, you may know what I'm talking about. If you think this print result is taken for granted, I think you can ignore this article, because what I want to talk about is concentrated in these lines, you have clearly understood the meaning behind it.

The following is my understanding for interested friends:

Intuitively, we can easily trust that after "A = B;", variable A points to the object B of class B. Naturally,. S should be equivalent to B. s, but this is not the case. When B inherits a, the field s of the parent class A is not replaced by the field s of B, but a copy is retained. The so-called override is for the method. Therefore, when we use new B (), the actually created object contains two versions of field s, one of which is "[a]" (belongs to Class) A "[B]" (belongs to Class B ). The gets () method has only one version. This is the difference between fields and methods in the inheritance process. That is to say, the concept of rewriting is irrelevant to the field. In row 3, we use. s accesses the s field of the Class A retained in the object B. In row 21, we changed the s field of the Class A version.

The essence of polymorphism is to dynamically determine the behavior of an object. The behavior of an object is embodied in methods rather than fields, and fields represent the state of an object. So there is only method polymorphism without field polymorphism. From the code above, no matter what type of variables you use to store the reference of object B, the method version that is finally called is the version of True Type B, which is the power of polymorphism.

From the compilation point of view, the s field in the above Code differs from the gets () method in that the S field is a static link during the compilation period (you can change its value, but its relative address in the object has been determined), while the gets () method is dynamically linked at runtime.

Having said so much, I really don't know whether I can express it clearly. After all, I have not studied Oo and compilation principles in the system. Please forgive me for anything wrong. Finally, please do not learn the encoding style here, because it is clear that we should execute the extract method Refactoring for the code segment in the main method. I am only here for convenience of annotation.

// BTW. It feels good to think about such a problem from time to time.

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.