Java programming ideology note (3) -- reuse of classes and reuse of programming ideology

Source: Internet
Author: User

Java programming ideology note (3) -- reuse of classes and reuse of programming ideology

I. Introduction to reuse Functions

Code reuse is not enough to copy and change the code, but also to use classes without damaging the existing program code.

In java, code reuse can be divided into the following three types: combination, inheritance, and proxy.

Ii. Combination

The new class uses the objects of the existing class. The new class consists of the objects of the existing class.

Iii. Inheritance

Declaration is implemented using the extends syntax.

Iv. Proxy

Java does not provide direct support for proxies, but indirect implementation is achieved through combination and inheritance. proxies are the moderate way of combination and inheritance. Proxy = inheritance + combination

For more information, see the relevant code!

5. Basic Method: quilt overload

The method used by the subclass to overload the parent class does not block any version of the base class. Therefore, the overload mechanism can be normally executed whether the method is defined in this layer or its base class.

6. Discussion on combination and inheritance

Emphasizing inheritance does not mean we should use inheritance as much as possible. On the contrary, we should use inheritance with caution. The clearest way to determine whether to use inheritance or combination is to ask whether you need to transform from a new class to a base 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.