Home > Java > javaTutorial > body text

Analysis of java modifiers

巴扎黑
Release: 2016-12-02 10:07:14
Original
1003 people have browsed it

Dear viewers, today we will discuss related issues about access modifiers in Java. This is also a common question in interviews.
Simply put, access modifiers are created by code writers to distinguish when the code can be accessed and when the code cannot be accessed. Access restrictions in Java are divided into four categories: friendly (the so-called default type), public, private, and protected.
Today we will take a look at the two categories that are more difficult to distinguish, namely friendly and protected.
First let’s look at friendly. When we do not add any modifiers in front of a member, the access level of the member is the default level. At this time, other classes in the same package as the class can access these members, while those in different packages Class cannot access these members, as follows:

package com.a1;
/**
 * A属于 com.a1 这个包
 * @author Will
 *
 */
public class A {
    //成员均为默认访问级别
int i;
String str;
void print(){
System.out.println("i:"+i+";str:"+str);
}
}
Copy after login
package com.a1;
/**
 * testA1属于com.a1
 * @author Will
 *
 */
public class testA1 {
public static void main(String[] args) {
//由于testA1与A属于同一个包下,所以可以访问友好型的成员
A a=new A();
System.out.println(a.i);
System.out.println(a.str);
a.print();
}
}
Copy after login
package com.b1;
import com.a1.A;
/**
 * testA2属于com.b1
 * 
 * @author Will
 * 
 */
public class testA2 {
public static void main(String[] args) {
// 由于testA2与A不属于同一个包下,所以不能访问友好型的成员
// 去掉注释符会报错
A a = new A();
//System.out.println(a.i);
//System.out.println(a.str);
//a.print();
}
}
Copy after login

Of course, readers may encounter a situation where when the package is not specified, class files in different folders can call each other's friendly members. The reason is: When no package is specified, all files belong to the default package by default, so friendly members can be called to each other.

Next, let’s take a look at the protected type. This type is very similar to the friendly (i.e. default) type, the only difference is inheritance. To put it simply, as long as the class file does not belong to the same package (including inheritance), other class files cannot access friendly members; but when the class inherits, regardless of whether it belongs to the same package, in the case of inheritance, Subclasses can access protected members of the parent class.

package com.a1;
/**
 * A属于 com.a1 这个包
 * i,str为友好型
 * j,str1为protected型
 * @author Will
 *
 */
public class A {
int i;
String str;
protected int j;
protected String str1;
public A(){
}
void print(){
System.out.println("i:"+i+";str:"+str);
}
protected void print(int k){
System.out.println("j:"+j+";str1:"+str1);
}
}
Copy after login
package com.a1;
/**
 * testA1属于com.a1
 * @author Will
 *
 */
public class testA1 {
public static void main(String[] args) {
//由于testA1与A属于同一个包下,所以可以访问友好型的成员
//也可以访问protected类型成员
A a=new A();
System.out.println(a.i);
System.out.println(a.str);
System.out.println(a.j);
System.out.println(a.str1);
a.print();
a.print(1);
}
}
Copy after login
package com.b1;
import org.junit.Test;
import com.a1.A;
public class ExtendsA extends A{
@Test
public void test(){
ExtendsA extendsA=new ExtendsA();
//当不再同一个包且继承情况下,不可调用父类友好型成员
//去掉注释会报错
//System.out.println(extendsA.i);
//System.out.println(extendsA.str);
//extendsA.print();
//当不再同一个包且继承情况下,可调用父类protected型成员
System.out.println(extendsA.j);
System.out.println(extendsA.str1);
extendsA.print(1);
}
}
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!