Access protection in JAVA

1. Access protection in Java

A class can only have two access levels: default and public. Public can be accessed by any other code, the default access control symbol can only be accessed by other codes in the same package

 

Content declared as public can be accessed from anywhere. Members declared as private cannot be seen outside the class. There is no explicit access statement, which is visible to subclasses or other classes in the package, which is the default access.

 

Use the import language bureau to introduce a specific class or even the entire package, and the class can be used directly by its name

Test code

package p1;

 

public class Demo

{

 

public static void main(String[]args)

{

//TODO Auto-generated method stub

 

Protectionob1 =new Protection();

Derived ob2  = new  Derived(); // Inherit Protection, execute Protection 's constructor first , and then execute your own

SamePackageob3 =new SamePackage();

}

 

}

package p1;

 

public class Derivedextends Protection

{

Derived()

{

System.out.println("derived constructor");

System.out.println("n = " +n);

// class only

// System.out.println("n_pri = " + n_pri);

System.out.println("n_pro = " +n_pro);

System.out.println("n_pub = " +n_pub);

}

}

package p1;

 

public class Protection

{

int n = 1; 

private int n_pri = 2;  

protected int n_pro = 3;

public int n_pub = 4;  

 

public Protection()

{

System.out.println("base constructor");

System.out.println("n = " +n);

System.out.println("n_pri = " +n_pri);

System.out.println("n_pro = " +n_pro);

System.out.println("n_pub = " +n_pub);

}

}

package p1;

 

public class SamePackage

{

SamePackage()

{

Protectionp =new Protection();

System.out.println("same package constructor");

System.out.println("n = " +p.n);

// class only

// System.out.println("n_pri = " + p.n_pri);

System.out.println("n_pro = " +p.n_pro);

System.out.println("n_pub = " +p.n_pub);

}

}

package p2;

 

public class Demo

{

 

public static void main(String[]args)

{

//TODO Auto-generated method stub

Protection2ob1 =new Protection2();

OtherPackageob2 =new OtherPackage();

}

 

}

package p2;

 

public class OtherPackage

{

OtherPackage() {

p1.Protectionp =new p1.Protection();

System.out.println("other package constructor");

// class or package only

// System.out.println("n = " + p.n);

// class only

// System.out.println("n_pri = " + p.n_pri);

// class, subclass or package only

// System.out.println("n_pro = " + p.n_pro);

System.out.println("n_pub = " +p.n_pub);

}

}

package p2;

 

public class Protection2extends p1.Protection

{

Protection2()

{

System.out.println("derived other package constructor");

// class or package only

// System.out.println("n = " + n);

// class only

// System.out.println("n_pri = " + n_pri);

System.out.println("n_pro = " +n_pro);

System.out.println("n_pub = " +n_pub);

}

}

package test;

 

class Box

{

double width;

double height;

double depth;

 

public Box(double w,double h,double d)

{

//TODO Auto-generated constructor stub

width =w;

height =h;

depth =d;

}

 

public String toString()

{

return "Dimensions are " +width +" by " +depth +" by " +height +".";

}

}

 

public class test1

{

 

public static void main(String[]args)

{

//TODO Auto-generated method stub

Boxb =new Box(10,12,14);

Strings ="Box b: " +b;// concatenate Box object

System.out.println(b);// convert Box to string

System.out.println(s);

}

 

}

Guess you like

Origin blog.csdn.net/zl1107604962/article/details/52489975