Thinking about Java construction class and object

Source: Internet
Author: User

Simply record the process in which Java constructs classes and objects, and the analysis of this and super for special cases.

First, then yesterday's question, I made a few variants:

Pic1. Original:

Pic2. The This keyword in the T.foo method is removed:

Pic3. Erased the B.foo method on the basis of the original:

Pic4. Erase the original B.foo method and remove the This keyword from the T.foo method:

These four graphs are fully tested for this and super two keywords, and the test results show that these versions of the program run the same way ...

Here, or insert a flowchart:

First, say the Super keyword, which is used when using the Super Keyword: call Super.foo in B.foo.

But the fact is, if there is no Foo method in B, the effect is the same as the one super.foo effect in B.foo.

We can think of it this way:Super.foo is to "throw" the B.foo execution into its parent class (super) to execute, but b if it does not show the declaration and implement the Foo method, its default behavior is still the Foo method that invokes the parent class of B.

So... Here, in class B, "void Foo {super.foo ();} "Statement, is a statement without any meaning (if I am a smart enough Java compiler, I will not talk about this statement, I just need to copy the parent class for the implementation of this method, at least in this program).

Here, the above four paragraphs of different code become two different code ...

According to the results of the program output preliminary judgment: Here the This keyword has no effect on the final result of the program.

The reason for this is easy to analyze, although there is a super keyword (which is easier to analyze without the Super keyword) and lets us jump into t to find the code, but the current object is still an instantiated Class B object, and Class B has implemented the Bar method, So when the program calls the bar method, it will naturally go from class B to find the Bar method implementation, found, then directly output "B.bar" (so we can even understand: in fact, before each method, there is a implied this keyword, such as method () In fact, it is This.method (), which shows that this is probably the mechanism of implementing the method in Java.

So how to make the program through small changes, without changing the inheritance relationship between T and B, so that the output into "T.bar" it?

The above analysis gives us no expectation of this keyword. Along the above analysis, if you want to find an implementation of the T.bar method, just comment out the bar method in B .

Of course, the other way of thinking is: since the output of T.bar, this method is the object of T class can do, then in Class B to get a T class object , like this ():

Here are two simple ways to change the code (some might think that the second method is more or less appropriate, after all, the output of the program is essentially a T-class instance object in Class B to help fix it).

Let's briefly describe the process of class and object construction.

To more generally represent the construction of a class, I draw a picture like this:

The arrows between each of the classes on the left side of this diagram represent extends, which are directed to the parent class by the subclass, and each class implements the same method as the color of the class.

Of course, the method inherited from the parent class in each subclass of the diagram is either preserved intact or overwritten with the actual implementation of the parent class. I did not draw an example of the method of overriding the parent class in this picture (at least the color of each method never changed), but this is actually quite common, and the fact that it is drawn naturally makes the picture more "colorful".

  At this point, let's think about it that the inheritance mechanism in Java is single-inheritance, but Java supports interface extensions, plus each class also contains member variables (or properties called classes), when such a large graph is described in a different color drawing, Many different instance objects will be colorful, which will make it easy for us to understand "what is polymorphic in Java".

Continue to summarize the above knowledge: in the process of instantiating an object by the constructor, it is implemented in the process of constructing from the parent class to the subclass step-by-step. Here's an example:

1 classGrandpa {2     protectedString status = "Grandpa";3     protected intAge = 70;4     voidfunc () {5System.out.println ("Grandpa.func");6     }7     voidWho () {8 System.out.println (status);9     }Ten     voidJump () { One          This. Func (); A     } - } -  the classFatherextendsGrandpa { - Father () { -Status + = "-Father"; -     } +     voidfunc () { -System.out.println ("Father.func"); +     } A     voidWho () { at         Super. WHO (); -     } -     voidJump () { -         Super. Jump (); -     } - } in  - classSonextendsFather { to Son () { +Status + = "-Son"; -     } the     voidfunc () { *System.out.println ("Son.func"); $     }Panax Notoginseng     voidWho () { -         Super. WHO (); the     } +     voidJump () { A         Super. Jump (); the     } + } -  $ classFamily { $      Public Static voidMain (String args[]) { -Son son =NewSon (); - son.who (); the Son.func (); - son.jump ();Wuyi     } the}
Family.java

The output of this program is:

Grandpa--Father and Son
Son.func
Son.func

It is also a good indication that the construction process is structured first-class (after all, the program does not simply output "grandpa--and Son").

As for the next two test methods and program output, it is much easier to determine who the current object is or whether it has a method that the program wants to invoke.

Since it is a first-level invocation of the constructor to instantiate an object, a constructor link is faulted, and an object is definitely not instantiated. For example, let the constructor prototype of the Father class be declared as Father (int require). At this point the compiler will gracefully tell us:

At this point, just put "super (666) in the constructor of son." Add as the first sentence. (It's more or less embarrassing to have a Super keyword statement that just doesn't work in the analysis program.) Of course, the 666 here is just a random number that can be replaced by any int type data. Because this is just a requirement of the Father Constructor (require), after all, this require is not used in the Father class.

To help super keyword "save some face", say two words I think the importance of super keyword:

    The Super keyword is actually the first sentence in the constructor, when the constructor is the default constructor (that is, the implementation constructor is not specifically declared in the program or the parent class constructor does not need any arguments to instantiate the object), the first sentence of the (stealth) default constructor is "super ();".

The Super keyword makes it easier to invoke the parent class's methods, and even allows us to access the member properties of the parent class more quickly.

These are the two days of the "Class and object" of the content of the simple "Review memory Notes", and through the teacher's questions, let me have a new understanding of this and Super keyword (truth, before (at least yesterday) really wrong.

With new ideas to update later, are kindly invited welcomes readers to suggest corrections.

Thinking about Java construction class and object

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.