Java Programming thought Learning (v) Reuse class

Source: Internet
Author: User
Tags export class

1. Succession and Combination

There are two ways to reuse a class: inheritance and composition. Inheritance is not much to say, the combination is directly in the class new object.

Arrays are also objects, and using arrays is one of the combinations.

2. Initialize the base class

When you create an object that exports a class, the object contains a child object of a base class. This sub-object is exactly the same as the object that is directly new to the base class, with the only difference being that one is outside and one is wrapped inside the exported class object.

In the Export Class builder, initialize is performed by calling the base class constructor. "In Java, all the constructors inside all the exported classes must call one of the constructors of the parent class or all the constructors inside all the exported classes must call a different constructor (either this class constructor or the constructor of the parent class), but eventually you must call one of the constructors of the parent class. That is, you must be sure that the parent class object is initialized correctly. If you do not explicitly call other constructors, the JVM defaults to adding code that calls the parent class without a parameter construct, and if the parent class has explicitly defined the other parameter constructs without defining an parameterless construct, it will report a compile-time exception that is not found. 】

3, Agent: between inheritance and combination of the "golden mean"

1) Place a member object in the class you want to build (similar combination)

2) Expose all methods of the member object in the new class (like inheritance)

4. Discussion on succession and combination

Combination is much more common than inheritance;

Inheritance techniques should be used with caution, and multiple combinations should be used. Because the combination of ways is much more flexible than inheritance.

So when should it be more appropriate to use inheritance? The following is the basis for judging:

Do you need to transform from a new class to a base class?

1) If the transformation must be upward, then the inheritance is necessary;

2) If you do not need to, you should consider the inheritance and combination of trade-offs.

5. Final keyword

Used to decorate data, methods, and classes.

Final state indicates the final state: (if it is a variable) is not modifiable, (if it is a method) is not overwritten, (if it is a class) is not inheritable.

Static Final "": Represents compiler constants;

The private method implies a final effect (because he is also not overwritten and modified, etc.).

Java Programming thought Learning (v) Reuse 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.