Access modifiers in Java

Source: Internet
Author: User
Tags modifier modifiers

The modifiers in Java have not been remembered, now combined with some information on the web to summarize, and focus on the protected modifier.

I. Access modifiers in Java

One of the basic concepts of Java object-oriented is to encapsulate the details and expose the interface. The Java language uses access control modifiers to control access to the methods and variables of classes and classes, exposing the interface to the consumer, but hiding the implementation details. Access control is divided into four levels:

(1)public: classes, generic variables and methods that are modified with public, and any class within and outside the package (including subclasses and ordinary classes) can be accessed;

(2)protected: Protected modified class, generic variables and methods, any class within the package and those inheriting the subclass of the class can access (explained here later), protected focus on inheritance;

(3)default: If a class, generic variables, and methods do not have any modifiers (that is, none of the public, protected, and private), then their access rights are default (access rights are defaulted). Implied

classes, generic variables, and methods that recognize access, any class within the package (including subclasses that inherit the class) can access it, and no class outside the package will be able to access it (including subclasses that inherit the class outside of the package). default Focus Highlight Package ;

(4)private: class, generic variables and methods that are modified with private, only this class can access, and any class outside the package cannot access it.

Some information on the web and some books on the Java access modifiers are clearly summarized in the table, as shown in the following table:

Access level

Access Control modifiers

Similar

Same package

Sub-class

Different packages

Public

Public

be protected

Protected

--

Default

No access control modifiers

--

--

Private

Private

--

--

--

I think that the table some problems are not clear, such as the access modifier protected, different packages can not be accessed, and the subclass can access, then ask in different packages of sub-class can be accessed or not access it? Therefore I am in the self

On the basis of understanding, for their own convenience, easy to remember, re-organized a table as follows:

different packages different classes

(excluding subclasses)

different buns class

TD width= "94" > TD width= ">
access level

Access control modifier

homogeneous

same package different classes (without subclasses)

with Bun class

Public

Public

be protected

Protected

--

√ (note)

Default

No access control modifiers

--

--

Private

Private

---

---

--

--

  Important summary: Through the above analysis, we can see:

1. Public, private and protected have no objection to us.

2. The top-level class can only be decorated with the public access modifier and the default access modifier, where classes decorated with the default modifier (and classes without any modifiers, such as Class b{}) cannot be inherited by classes in other packages. This also indicates that the default access modifier highlights the package permissions

3. Protected: I did an experiment, found in the subclass of the different packages, new A parent class object, and use the parent class object to access the parent class with protected decorated generic variables and methods can not be accessed, and new a subclass object, Subclass objects can be accessed (the class that describes the protected adornment can be inherited by classes in other packages). You can also use the Super keyword call in a method of a subclass override parent class. Does this conflict with the summary (red tick) in the table above? I am also baffled by the solution. Finally, a relatively more acceptable explanation was found on the Internet, as follows:

 The decorated member variables and methods of the protected modifier are also known as protected member variables and methods, and protected member variables and methods can be accessed through instances of the class in this class or in other classes in the same package, including subclasses, or by classes in the same package or classes in different packages. However, it cannot be accessed through an instance of the class in other classes (including subclasses) in different packages.

4. If a class uses public adornments, the class name of the class must be the same as the name of the source file where he resides. A. Java source file has only one public class, and the top-level class can only be decorated with public and default modifiers (that is, no modifiers);

5. The final decorated class cannot be inherited, and there are no subclasses.

6. Abstract modified classes cannot be instantiated, they must be inherited by the quilt class. A class must be an abstract class as long as it has an abstract method, but abstract classes do not have to be abstract.

The final summary, in a word: The class that the protected modifier modifies (the parent class) is a member variable and method that can only be accessed by the quilt class, not whether the pipe class is in the same package as the parent class. The generic member variables and methods that are decorated by the default modifier can only be accessed by other classes in the same package, regardless of whether other classes are subclasses of the class. protected belongs to the subclass restriction modifier, and default is the package restriction modifier.

Access modifiers in Java

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.