I read the Spring Data source and meet a question: JdbcTemplate has a method:
JavaScript
x
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
JavaScript
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
JavaScript
public abstract class A {
@Nullable
private Integer a;
}
public class B extends A {
public void setA(@Nullable Integer a) {
this.a = a; <-- Wrong
}
}
Advertisement
Answer
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
JavaScript
public JdbcTemplate(DataSource dataSource) {
setDataSource(dataSource);
afterPropertiesSet();
}
And setDatasource method is in JDBCAccessor.