Thinking in Java (V)-composition and inheritance

Source: Internet
Author: User

Here we will mention initialization. The reference is not provided with the default object, and its value is null.

 

The base class object is encapsulated in the derived class object.

Therefore, the safest way to use Java is to first call the baseclass constructor in the derivedclass constructor.

 

The upward transformation mechanism also shows that after inheritance, the new class is a form of the old class.

 

Final keywords:

When it acts on the reference, it modifies the reference itself, so that it will end up with an object.

Maybe you want to change the corresponding object to final. Unfortunately, Java does not provide this mechanism.

In addition, the final method will not be rewritten in derived class.

 

You can overwrite a private function that will not be inherited,

In fact, what you do is to define a new function, because there is no function with the same name in the interface.

 

Finally, I would like to give a piece of advice: Oop should first consider combination and use inheritance only when necessary.

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.