やっぱり滑りに行った帰りに、ふと思ってみたので確認。
import java.io.Serializable; import java.lang.reflect.Method; public class ReflectTest3 implements Serializable { private static final long serialVersionUID = -6049702009069313042L; protected int value = 0; public void writePublic() { System.out.println("ReflectTest3#writePublic/"+ this.value); } protected void writeProtected() { System.out.println("ReflectTest3#writeProtected/" + this.value); } private void writePrivate() { System.out.println("ReflectTest3#writePrivate/" + this.value); } public static void main(String[] args) { new ReflectTest3().test(); } public void test() { try { ReflectTest3A testA = new ReflectTest3A(); Method writePublicMethod = ReflectTest3.class.getDeclaredMethod("writePublic", (Class[]) null); writePublicMethod.invoke(testA, (Object[]) null); Method writeProtectedMethod = ReflectTest3.class.getDeclaredMethod("writeProtected", (Class[]) null); writeProtectedMethod.invoke(testA, (Object[]) null); Method writePrivateMethod = ReflectTest3.class.getDeclaredMethod("writePrivate", (Class[]) null); writePrivateMethod.invoke(testA, (Object[]) null); } catch (Exception e) { e.printStackTrace(); } } public class ReflectTest3A extends ReflectTest3 { private static final long serialVersionUID = -6651000889570196353L; public ReflectTest3A() { super(); this.value = 1; } } }
確かに継承元のクラスのインスタンスは存在せんのじゃけぇ、こういう結果になるわな。
ReflectTest3#writePublic/1 ReflectTest3#writeProtected/1 ReflectTest3#writePrivate/1