Four access modifiers in Java the whole process of distinguishing and elaborating

Source: Internet
Author: User
Tags modifiers

Client programmer: A class consumer who uses the data type in its application, his goal is to collect a variety of classes for rapid application development.

Class Creator: A programmer who creates a new data type, with the goal of building the class.

The reason for the existence of access control: A, so that the client programmer can not touch the parts they should not touch; b. Allows the library designer to change how the class works internally without worrying about affecting the client programmer

Java Four keywords: public, protected, default, private (they decide what can be used to define what is immediately followed)

The smaller the scope < scope of access, the higher the security >

Access Rights class Bun class other packages

Public∨∨∨∨ (available to anyone)

Protect∨∨∨x (inherited classes can be accessed and have the same permissions as private)

DEFAULT∨∨XX (Package access, which can be accessed throughout the package)

PRIVATE∨XXX (elements that cannot be accessed by anyone other than the type creator and the internal method of the type)

Below, detailed in code (build four class person Student Teacher parents and a test class)

Package com.zq.demo.test1;
/**
* In-Class
* @author Administrator
*/
public class Person {
Public String uname = "Harry";

public void Introducemyself () {
System.out.println (uname);
}
}

Package com.zq.demo.test1;
Same package
public class Student {
Person p = new person ();
public void Test () {
System.out.println (P.uname);
}
}

Package com.zq.demo.test1;
Sub-class
public class Teacher extends person{
public int age;
Person p = new person ();
public void Test1 () {
System.out.println (P.uname);
}
}
Package com.zq.demo.test2;
Different packages
Import Com.zq.demo.test1.Person;

public class Parents {
Public String uname = "haha";
Person p = new person ();
public void Test2 () {
System.out.println (P.uname);
}
}

The result of the test class shows that public can access the

Test protected with the same four classes

After changing the uanme in person to protected class parents, the field person.uname is not visible (the description cannot span the package)

Three other non-impact

Description In addition to cross-package other does not affect access

Private

Access is available only in this class, but with the set, get method in the person class about uname

Default

Test result description can only be accessed in this package in other packages even if an inheritance relationship is not accessible

Four access modifiers in Java the whole process of distinguishing and elaborating

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.