Java Programming thought Note (iii)-Reuse of classes

Source: Internet
Author: User

A: Introduction of reusable functions

The reuse of code, in addition to copying and changing the code is not enough, in addition to the ability to use the class without breaking the existing program code.

Code reuse in Java can be divided into the following three categories: combination, inheritance, proxy.

Two: Combination

The new class uses an object from an existing class, and the new class consists of an object of an existing class.

Three: Inheritance

Declaration is implemented by extends syntax.

Four: Agent

Java does not provide direct support to the agent, but by combining and inheriting the indirect implementation, the broker is the middle of the combination and inheritance. Agent = inheritance + combination

Specific understanding can refer to the relevant code!

Five: base class method Quilt class overloading

A subclass overloads a method of a parent class and does not block any version of it in the base class, so the overloaded mechanism can execute normally regardless of the method definition in that layer or its base class.

Six: Re-discussion on combination and inheritance

Emphasizing inheritance does not mean that we want to use inheritance as much as possible, instead we should use inheritance with caution. One of the clearest ways to determine whether to use inheritance or use a combination is to ask yourself if you need to move up from a new class to a base class.

Java Programming thought Note (iii)-Reuse of classes

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.