Java Programming Idea: 7th Chapter Reuse Class

Source: Internet
Author: User
Tags export class

Reusing code is one of the many compelling features of Java. But it's not enough to replicate the code and change it, but more needs to be done.

Two forms of multiplexed code:

1. Grouping, generating existing class objects in a new class

2. Inheritance, creating new types with existing types

7.1 Combination Syntax

7.2 Inheritance Syntax

7.2.1 Initializing a 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 the same as the object you created directly from the base class (Java automatically inserts a call to the base class constructor in the export class constructor, the base class contains only the parameter constructs that need to be called with Super, not omitted), and the difference is that the child objects of the inheriting-date class are wrapped inside the exported class object. and directly created to come with the outside.

7.3 Agents

Proxy: The third relationship, Java does not have direct support for it, inheritance and composition between the mean.

Implement proxy: Place a member object in the proxy class (just like a combination), exposing all methods of the object (like inheritance) in the new class.

7.4 Combining and inheriting using combinations

7.4.1 ensure proper cleanup

You can't rely on the garbage collector to do anything other than memory. If you need to clean up, it is best to write your own cleanup method (the subclass cleanup method and the parent class cleanup method call order is executed according to the creation of the reverse order), after the end of the object, use the finally{} clause to invoke the cleanup method, do not use Finalize ().

7.4.2 Name Masking

Subclasses can override and overload a base class method. Use @override annotations to prevent accidental writes of overloading when you want to overwrite

7.5 Choose between combination and foundation

Inheritance: is-a

Combination: Has-a

7.6protected keywords

Provide child classes and access to the same package

7.7 Upward transformation

Providing a method for a new type is not the most important aspect of inheritance technology, the most important of which is the relationship between the new class and the base class, which can be represented by a "new class is a type of an existing class."

Why 7.7.1 is called upward transformation

7.7.2 on how to choose combination and inheritance

Ask yourself if you need to transform from a new class to a base class, you need to use inheritance, or do not use it as much as possible

7.8final keywords

Refer to the Java final keyword & parameter passing features

7.9 initialization and loading of classes

The loading of classes in Java occurs at first use, usually by creating objects or accessing static methods (constructors are also static methods, only implicitly).

7.9.1 Inheritance and initialization

Java Programming Idea: 7th Chapter 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.