Skip to content

Extension private field in java

I read the Spring Data source and meet a question´╝Ü JdbcTemplate has a method:

public void setDataSource(@Nullable DataSource dataSource) {
    this.dataSource = dataSource;

and I find that this.dataSource is from it`s father class JdbcAccessor.declearing as follow

private DataSource dataSource;

my question is: why extension class JdbcTemplate can access its father class`s private field? I try to use it as follow and find IDE show its wrong

public abstract class A {
    private Integer a;

public class B extends A {

    public void setA(@Nullable Integer a) {
        this.a = a;    <-- Wrong



It can not. Most likely you used some sort of decompiler which couldn’t properly decompile it. The actual code is like this in JDBCTemplate

public JdbcTemplate(DataSource dataSource) {

And setDatasource method is in JDBCAccessor.

User contributions licensed under: CC BY-SA
3 People found this is helpful