Java Review Note 7--java package

Source: Internet
Author: User

Java Package

Getter () & Setter ()

There are three major benefits to using encapsulation:

1, Good package can reduce the Coupling.

2, the structure within the class can be freely modified.

3, the member can be more precise control.

4, Hide information, Realize the Details.

Java Inheritance

Inheritance is the technique of building a new class using the definition of an existing class, and the definition of a new class can add new data or new functionality, or it can use the functionality of the parent class, but not selectively inherit the parent class. By using inheritance, we can easily reuse the previous code, which can greatly improve the efficiency of development.

In fact, the successor is the specialization of the successor, in addition to having the characteristics of the successor, but also has its own unique characteristics. For example, cats have the characteristics of catching mice, climbing trees and other animals. At the same time in the inheritance relationship, the successor can be completely replaced by the successor, and Vice versa, for example, we can say that cats are animals, but can not say that animals are cats is the reason, in fact, we call it "upward transformation"

    • Constructor: for constructors, It can only be called, not Inherited. Call the construction method of the parent class we can use Super (). The compiler defaults to calling the constructor of the parent class for the Subclass. This default call to the constructor of the parent class is a prerequisite: the parent class has a default Constructor. If the parent class does not have a default constructor, we will have to show the use of Super () to call the parent class constructor, or the compiler will error: cannot find a constructor that conforms to the parent class.

    • Protected keywords

    • Upward transformation

1, the parent class changes, the subclass must Change.

2, inheritance destroys the encapsulation, for the parent class, its implementation details are transparent to the SUBCLASS.

3, inheritance is a strong coupling relationship.

Ask yourself if you need to transform from subclass to parent class. If you have to move up, inheritance is necessary, but if you don't, you should consider whether you need to inherit

The Finalize-method Name. Java technology allows the use of the Finalize () method to do the necessary cleanup before the garbage collector clears objects from Memory. This method is called by the garbage collector to this object when it determines that the object is not Referenced. It is defined in the Object class, so all classes inherit it. Subclasses override the Finalize () method to organize system resources or perform other cleanup Work. The Finalize () method is called on the object before the object is deleted by the garbage Collector.
Super &this

Super (name);//call the parent class with the same formal parameter construction method

This (name);//call the constructor that currently has the same formal parameter

This is a pointer to the object itself

The similarities and differences between Super and this :

  • Super (parameter): call one of the constructors in the base class (should be the first statement in the Constructor)

  • This (parameter): calls another form of the constructor in this class (should be the first statement in the Constructor)

  • Super: it refers to a member in the immediate parent class of the current object (used to access member data or functions in the parent class that is hidden in the immediate parent class, when the base class has the same member definition as in the derived class, such as super. variable name super. member function by name (argument)

  • This: it represents the current object name (where It is easy to generate two semantics in the program, you should use this to indicate the current object, if the Function's shape participates in the class member data has the same name, this should be used to indicate the member variable Name)

  • The call to Super () must be written on the first line of the subclass construction method, Otherwise the compilation does not pass. the first statement of each subclass construction method is implicitly called Super (), and if the parent does not have this form of constructor, it will be error-free at compile Time.

  • Super () is similar to this (), except that super () calls the constructor of the parent class from the subclass, and this () invokes other methods within the same class.

  • Super () and this () all need to be placed in the first row within the construction method.

  • Although you can call a constructor with this, you cannot call Two.

  • This and super can not appear in a constructor at the same time, because this is bound to call other constructors, the other constructors will inevitably have a super statement exists, so in the same constructor has the same statement, it loses the meaning of the statement, the compiler will not pass.

  • Neither this nor super can appear in the static methods and code BLOCKS.

Java Review Note 7--java package

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.