class inherits the meaning of super () to write a correct exception class

Source: Internet
Author: User

All these things I read many famous teacher courses and the results of their own research, is strictly forbidden to reprint, here points out how to correctly define an exception class and take a look at the Java code written by Sun
Topic One: Does the constructor execution of a subclass necessarily accompany the construction of the parent class?

1.this () and super () cannot coexist

2. If the This () and super () are not displayed, the creation of the subclass object must perform the construction of the parent class

3. Since super () and this () do not coexist, and say that the construction of the child class will certainly perform the construction of the parent class, then I let the construction of the child class do this () is not the construction of the parent class can not be executed?


4. If I do not allow the construction of the parent class to execute, I invoke the parameter "this" (int age) by means of the subclass without argument, and then have the argument to call the "this" () without the argument, and now the system cannot call
Super () Yes, but!!! Direct error

The construction of the summary subclass, as long as it is executed, must accompany the execution of the parent class's construction

Topic Two: Why design a subclass's structure must accompany the parent class's passive constructs to execute

1. First, we go back to the definition of constructs: creating objects and giving the class members initial means that's what it's all about. So what does a subclass call a parent class construct?
Answer: Subclasses contain members inherited from the parent class, and here we understand that each subclass object contains a parent type object, and we can initialize the object of the parent type in the subclass with Super (), which can improve the reuse of the code.

But it doesn't make much sense: I don't care about that. Code Snippets
So I'm going to give you an example, if the member in the parent class is private and you can only assign a value to a private member through Super (), why assign a member to a private member of the parent class
Here's an example:


I can see an exception I wrote myself: It was thrown by the compiler. And with the thrown write error hint, show myexception (I wrote) inherited from its parent class exception (System-written)
This field is used to hold the "age illegal" string, but it is private in the parent class, and you can only assign it this way.


Obviously the error message you gave was not thrown by the compiler at all. Specifically, to see the Java write source code
Obviously only the private field in the parent class is assigned a value, and the exception class construct of a parameter you write makes sense.
First we only look at the construction of a parameter of the parent class (because I only call it), enter exception to find only one argument to construct

It is visible that exception also calls the construction of its parent class

Not in call super (); so it's already up to the top, apparently you wrote that "age illegal" and finally saved it to Detailmessage.
Look at the definition of it.
It is private, and the subclass of the private object in the parent class cannot be accessed directly, obviously only by calling Super (); exception in Java is doing it himself.

class inherits the meaning of super () to write a correct exception class

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.