Skip to content

Instantly share code, notes, and snippets.

@sovietspy2
Last active September 28, 2023 14:33
Show Gist options
  • Save sovietspy2/f743876fb21073f9b6a7350139c3d4e5 to your computer and use it in GitHub Desktop.
Save sovietspy2/f743876fb21073f9b6a7350139c3d4e5 to your computer and use it in GitHub Desktop.
much java so safe
package org.example.inheritance;
import java.lang.reflect.Field;
public class Main2 {
public static void main(String[] args) {
Child child = new Child();
child.list();
}
public static class Parent {
private Integer value = 1;
Parent() {
System.out.println("Parent ()");
}
}
public static class Child extends Parent {
Child() {
super();
}
public void list() {
Field[] fields = getClass().getSuperclass().getDeclaredFields();
// Make private fields accessible and print their names
for (Field field : fields) {
field.setAccessible(true);
System.out.println("Field Name: " + field.getName());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment