Access Restrictions in Java

Source: Internet
Author: User

Package: Packet access rights

If no access modifier is provided, it means that it is a package access permission .

The default access permission does not have any keywords , but it usually refers to package access rights (sometimes also expressed as friendly). This means that all other classes in the package can access this member or method, but all classes outside of the package are not accessible .

Cases:

Com.notes.packages.test.Info

Package com.notes.packages.test;

Public class Info {

void print () {System. out. println ("default Method--print ()"); }

}

Com.notes.packages.test.PublicDemo01

Package com.notes.packages.test;

Public class PublicDemo01 {

Public static void main (string[] args) {

Info x = new info ();

X.print ();

}

}

PublicDemo01 and info under the same package, you can access the method--print () of the default level of info.

Com.notes.packages.PublicDemo02

Package com.notes.packages;

import Com.notes.packages.test.Info;

Public class PublicDemo02 {

Public static void main (string[] args) {

Info x = new info ();

X.print (); Error

}

}

PublicDemo02 and info are not under a package and can not access info's package access level Method--print ().

Public : Interface access rights

Using The public keyword means that the declared member or method is accessible to everyone .

Example: if the print () method permission in the default Level permission example is set to public, PublicDemo02 can access it.

Package com.notes.packages.test;

Public class Info {

Public void print () {System. out. println ("Public Method--print ()"); }

}

Private: Unable to access

Using the private keyword means that the declared member or method, except for this class, cannot be accessed by any other class .

Application Scenario: Single-case mode

protected: Inheriting access rights

A new class (called a subclass or derived class) can reuse an existing class (called the parent or base class) through inheritance, and then extend the members, methods of the base class. Sometimes, the creator of a base class wants a particular member to give its access to a derived class instead of all classes. Public cannot do this, and for this reason, protected was introduced to complete the work. protected also provides package access, which means that derived classes and other classes within the same package can access protected members or methods .

Example: After a subclass inherits from a parent class, it can access the protected member of the parent class.

class Father {

Private String a = "private";

protected String B = "protected";

Public String C = "public";

};

class Son extends Father {

Public void print () {

System.out.println ("element A:" + super.a); Error

System. out. println ("element B:" + super. b);

System. out. println ("element c:" + super. c);

}

}

Public class ProtectedDemo01 {

Public static void main (String args[]) {

Son sub = new son ();

Sub.print ();

}

};

Summary

The previous examples show that the members and methods of a class can be decorated with various permission modifiers.

In addition, there are some points to note:

(1) The use of a static member, the permission modifier of a static method, is the same as an ordinary member or method.

(2) Although the class can also be modified by modifier words, but can not use private,protected two permission rhetoric words.

(3) In some books, the access to the package is also called the default access permission. It is not recommended for individuals to remember this because it is easy to confuse the new feature in Java Se8-thedefault keyword. This keyword can only be used for Interface, which allows the programmer to define the default implementation of the interface in Interface (the previous JDK version is not allowed, you can only declare the method in the interface).

Access Restrictions 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.