アクセス権の再確認

1617 ワード


package com.chl.pone;

public class Father {
	private int privateValue;
	int friendlyInt ;
	protected int protectedValue;
	public int publicValue;
}

package com.chl.pone;

public class ClassSamePackage {
	public void publicMethod(){
		Father father = new Father();
		father.friendlyInt = 1;
		father.protectedValue = 1;
		father.publicValue = 1;
	}	
}

package com.chl.pone;

public class ChildSamePackage extends Father{
	public void publicMethod(){
		this.friendlyInt = 1;
		this.protectedValue = 1;
		this.publicValue = 1;
	}
}

package com.chl.ptwo;

import com.chl.pone.Father;

public class ChildNotSamePackage extends Father{
	public void publicMethod(){
		this.protectedValue = 1;
		this.publicValue = 1;
	}
}

package com.chl.ptwo;

import com.chl.pone.Father;

public class ClassNotSamePackage {
	public void publicMethod(){
		Father father = new Father();
		father.publicValue = 1;
	}
}

以下にまとめます.
アクセス権
本類の内部は同じ包(子類またはその他の類)の異なる饅頭類のどこでも
private       OK                  
friendly      OK          OK         
protected     OK          OK                   OK
public        OK          OK                   OK           OK