Java object-oriented programming--advanced concepts of the eighth class

Source: Internet
Author: User

8.1 Access Modifiers:

Public: This class or non-class is accessible;

Private: Only the class can be accessed;

Protected: Members of the class and its subclasses can be accessed, and classes in the same package can be accessed;

Default: Classes in the same packet can be accessed.

The purpose of the access modifier is to limit the range of properties and methods that are used and called.

8.2 Package

Information is organized in accordance with the principle of information hiding. The package has two characteristics: the collection of information, the hiding of information.

8.3 accessor get () and modifier set ()

The difference between a private property +get/set () and a public property:

Private attribute +get/set (), you can make a read-only or write-only property by removing the Get or set method, but the public property cannot.

A private property can verify the validity of a property value by doing some validation before the set method sets the property value. However, the public attribute cannot be done.

8.4 Static blocks and instantiation blocks

public class x{

static{}

} is called a static block that executes when the class is loaded and executes only once.

Publick Class x{}

{} is called an instantiation block, and each time an object is generated, an instantiation block is executed, followed by super ().

When an object is instantiated and the class contains an instance initialization block, the following events occur sequentially:

    • The corresponding constructor in the subclass is called;
    • To perform a call to Super, the control flow jumps to the corresponding parent class constructor;
    • After the parent class constructor executes, the control flow jumps back to the subclass constructor;
    • The instance initialization block executes before any statements after super () in the subclass constructor are executed;
    • Finally executes the statement after Super () in the subclass constructor

8.5 Internal Classes

In Java, you can also define classes inside a class. This class, defined inside a class, is called an inner class. The class in which the inner class resides is called an external class.

An inner class is equivalent to an attribute of an outer class, and an object of an inner class must depend on an object of the outer class.

The class file name generated by the internal classes is "external class name $ internal class name."

Java object-oriented programming--advanced concepts of the eighth class

Related Article

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.