Function One of the Java modifier (public protected default private group)

Source: Internet
Author: User
Tags modifiers

1.public protected Default Private group

Public permissions are the largest, homogeneous, same package, different packages, with the Bun class between the parent class, different buns class can be accessed between the parent class.

Java defaults to default, that is, friendly (note: Friendly is not in Java, is the C argument.) Similar, same package, with the Bun class parent class can be accessed.

Protected protected, homogeneous, same package, with Bun class parent class, different buns class parents can access

Private permissions are minimal and homogeneous can be accessed.

The figure is as follows:

Location private default protected public
The same class Yes Yes Yes Yes
Classes within the same package no Yes Yes Yes
Subclasses in different packages no no no yes Yes
Different packages and not subclasses no no no no yes

Public access is highest, whether it is the same package or whether it is a subclass that can be accessed
Protected is not accessible except for different packages and not subclasses, others can be
The default level is second, requiring only classes in the same package to access
Private can only be accessed by the same class

These modifiers can be used to modify methods or properties, but the class can only be public or not written.

Instance:

Package test;

Use of access modifiers
public class publicclass{

Public String publicvariable = "publicvariable";
Private String Privatevar = "private Var";
protected String Protectedvar = "protected var";
String defaultvar= "Defaultvar";

public void Showpublic () {
System.out.println ("Showpublic method!");
}

public void showprotected () {
System.out.println ("Show protected method!");
}

void Showdefault () {
System.out.println ("Show default method!");
}

private void Showprivate () {
System.out.println ("Nobody would access!");
}

}


Java code
Package test;

Import Test. Publicclass;
Class testpublic{
Private String var = "Private variable in class-test!";

public static void Main (String args[]) {
Testpublict = new Testpublic ();
Publicclass PClass = new Publicclass ();
The common properties and methods in another class are accessible to the outside world.
System.out.println ("Resource properties accessible to:" +pclass.publicvariable);//accessible
Pclass.showpublic (); Access to
The following two compilation errors, private methods in another class are not accessible
System.out.println ("Resource properties accessible to:" +pclass.privatevariable); Cannot access
Pclass.showprivate (); Cannot access
Private variables can be accessed by themselves
SYSTEM.OUT.PRINTLN ("Private variable can be accessed by itself" +t.var);
Protected members can be accessed by members of the class and by a member of the subclass.
can also be accessed by other class members in the same package
System.out.println ("Resource properties accessible to:" +pclass.protectedvar);//accessible
Pclass.showprotected ();//accessible
System.out.println ("Resource properties accessible to:" +pclass.defaultvar);//accessible
Pclass.showdefault ();//accessible
}
}

Function One of the Java modifier (public protected default private group)

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.