Java-based super and uper

Source: Internet
Author: User

Java-based super and uper

I have briefly explained the basic inheritance usage. Now I want to add something that is easily overlooked in inheritance, such as super.

 

, I added an output statement to the constructor of the parent class (Father), but I did nothing in the Child class .. however, when the new subclass object is used, the content in the parent class constructor is output... why ??

Are you confused? I may have used a fake compiler.

Okay... I got the answer... because there is an implicit super () in the constructor ()...

These two classes mean the same...

Okay. Let me explain the super method.

The Chinese interpretation of super is the specific meaning of 'super', that is, the method of calling the parent class. for example, as we mentioned earlier, this is the method and its attribute of the call object, and super is the parent class method and attribute of the call object.

  

Super () is the constructor that calls the null parameter type of the parent class. when the constructor of the subclass is encountered, and then super () is encountered, the content in the constructor of the null parameter of the parent class is executed.

Here, remind me once .. in the subclass constructor, there is a super () by default, which means to call the null parameter constructor of the parent class .. but .. what if you have defined the constructor of the parent class...

Found no .. an error is reported directly .. in java, each class has an empty parameter constructor by default, such as Father () {}, but after you define the constructor (Father (String args) {}). The default constructor is missing .. if necessary, you can only define it by yourself.

The constructor of each subclass has a default super method. the default super () method is used to access the null parameter constructor of the parent class. as a result, the empty parameter constructor of the parent class is missing .. for this. the default super () method ... it's desperate, too .. then I made a protest (an error was reported ).

How can we solve this problem? There are two solutions:

1: Define an empty parameter constructor ..

This is a method... it's easy to understand, right... just define one more...

2: manually define the super statement...

 

See it .. replace the default super () with super (String ). I can see it at a glance .. when super calls the constructor of the parent class, it uses parameters to identify the constructor to call .. I won't say much about drawing a picture.

Let's talk about super calling the constructor ....

Next, let's talk about another usage of super... call the attributes and methods of the parent class.

OK... This is useless. super... because the subclass rewrites the show method of the parent class. So we printed the items in the subclass function directly... let's take a look at super.

Look .. we use super in the subclass show method. show (), directly call the show method of the parent class .. so I printed out the show method of the parent class... no.

Of course... super call method... I 've talked about... so it's the same to use super to call the attributes of the parent class...

 

Are you welcome to raise your question in the comment area ....

 

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.