Advanced concepts for the java--class

Source: Internet
Author: User
Tags modifiers naming convention

Each member of a class (including variables, methods, and constructors) has an access modifier that determines who can access the member. Java provides four levels of access for members of a class, arranged in the following order of access from large to small:

Exposure level: Use the Public keyword adornment. Members that are decorated with the public keyword are exposed externally, that is, public members can be accessed by any other object.

Protected Access level: Use the protected keyword decoration. Protected members can be accessed by classes in the same package, and can also be accessed by subclasses of the class, regardless of whether the subclass is in that package.

Default access level: No access modifier. Members of the default access level can be accessed by other classes in the same package.

Private access level: decorated with the private keyword. It is the lowest level in the four access modifiers. Private members are accessible only to the class itself, not to the public.

The class itself also has an access modifier. However, the access modifiers of the class are only common (declared with the keyword public) and the default (no access modifier keyword).

Encapsulation is the technique of making member variables in a class private, and providing public methods to access these member variables. If a member variable is declared private, it cannot be accessed by other classes, thus hiding the member variables in the class. Therefore, encapsulation is also known as data hiding. Encapsulation has many benefits, including:

The member variables of a class can be read-only or write-only.

A class can have a holistic control over what is stored in its member variables.

The user of the class does not need to know how the class stores the data.

Inheritance, encapsulation, polymorphism and abstraction are the four basic concepts of object-oriented programming.

Using the get and set named accessor methods and modifier methods is a widely used and recommended Java naming convention.

The keyword static allows a member variable or method not to be associated with a specific instance of a class. Customs key The member variable or method of the static declaration can be considered global, and any other class can directly access the member variable or call a static method without requiring an instance of the class.

A static member of a class is often referred to as a class member because a static member is associated with a class, not an instance of the class. Non-static member variables and methods are often referred to as instance members because non-static member variables and methods exist only in instances of the class.

Static member variables and methods cannot be accessed using references because references refer to instances of the class, and we do not need an instance of the class to access static members. To access a static member, use the class name.

Static initialization blocks are executed when the class is loaded. Instance initialization blocks are executed when the class is instantiated and called between constructor calls.

The inner classes in Java can be divided into four types, namely: ① static inner Class (as a static member of a class, which exists inside a class), ② member inner class (as a member of a class, exists inside a class), ③ local inner class (a class that exists inside a method), ④ anonymous inner class (which exists inside a class). But class with no class name).

Super keyword

This is a pair, very similar in usage.

Suoer. (The parent class object of the current object)

This. (Current object)

Super () invokes the specified method of the current parent class

This () invokes the current

Super () is just like this () and can only be written in the construction method, and can only be written in the first sentence

Super () is mainly used when overriding the method of calling the parent class

This () is called a method of the subclass

this keyword

The ability to access the parent class can also access the child class public

But super can only access the parent-class common

So at the moment, super is less than this.

Final keyword

The method cannot be overridden when final modifies a method

When the final modifier is tired, the class cannot be inherited, and this class is called the final class

When final modifies a variable, the variable cannot be modified

String This class is final

Advanced concepts for the java--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.