Four types of access modifiers in Java

Source: Internet
Author: User
Tags modifiers

Four types of access modifiers in Java: Public, Protected, default (no modifiers, defaults), private.

    • Four modifiers that can be modified (class, method, member variable)
Public Protected Default Private
Class X x(inner class can be decorated with private)
Method
Member variables

    • Access permissions for four modifiers

Public: Visible to all classes

Protected: Visible to classes in the same package, to the same package, and to subclasses in different packages

Default: Visible to classes in the same package, to subclasses in the same package

Private: Visible only to the class itself

Visible is accessible meaning that the components (classes, methods, member variables) that are decorated by these modifiers can be accessed by other classes. A subclass can be seen to be inherited by a child class.

It is important to note that the difference between protected and default is that subclasses in different packages can inherit.

Access rights Class Same package Subclasses in the same package Subclasses in different packages Different packages
Public
Protected X
Default X X
Private X X X X

    • For example

Package1:parentclass, Childclassone, Testone

Package2:childclasstwo, Testtwo

Package3:testthree

where ParentClass is

public class ParentClass {public int a=2;protected int b = 3;int c = 4;private int d = 5;}

  1. for Testone, ParentClass classes, A, B, and C are visible, and D is not visible. That is, you can new a ParentClass object in Testone, and the A,B,C variable in the object can be manipulated. As follows:

public class Testone {public static void main (String args[]) {ParentClass obj = new ParentClass (); System.out.println (OBJ.A); System.out.println (OBJ.B); System.out.println (OBJ.C);//system.out.println (OBJ.D);     D not visible}}

  2. Childclassone inherits ParentClass and is in the same package, A, B, c are visible, D is not visible.

public class Childclassone extends parentclass{public static void main (String args[]) {Childclassone obj = new Childclasso NE (); System.out.println (OBJ.A); System.out.println (OBJ.B); System.out.println (OBJ.C);//system.out.println (OBJ.D);      D not visible}}

3. Childclasstwo inherit ParentClass, then a, b are visible, D is not visible, because the subclass and ParentClass are not in the same package, so C (default type) is not visible.

public class Childclasstwo extends parentclass{public static void main (String args[]) {Childclasstwo obj = new Childclasst Wo (); System.out.println (OBJ.A); System.out.println (OBJ.B);//system.out.println (OBJ.C);    C not visible//system.out.println (OBJ.D);    D not visible}}

4. For Testtwo, the ParentClass class, A are visible, B, C, d are not visible. Since Testtwo and ParentClass are not in the same package, only public-modified components are visible.

public class Testtwo {public static void main (String args[]) {ParentClass obj = new ParentClass (); System.out.println (OBJ.A);//system.out.println (OBJ.B);     b not visible//system.out.println (OBJ.C);     C not visible//system.out.println (OBJ.D);     D not visible}}

 

    • It is also worth noting that the visibility between the subclasses of the inherited parent class and the other classes depends on the visibility of the other classes to the parent class.

For Testone, Childclassone in A,b,c are visible, D is not visible, this is because Testone and ParentClass in the same package.

For Testthree, only a visible in Childclassone is due to testthree and ParentClass in different packages.

Need to note:

Although a, B to childclasstwo visible, Testtwo and Childclasstwo in the same package,

But for Testtwo, only a is visible in Childclasstwo, because Testtwo and ParentClass are in different packages, only public adornments are visible.

Four types of access modifiers in Java

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.