I often see constructors like this
public class A {
private int b;
public A(int b) {
setB(b);
}
public void setB(int b) {
this.b = b;
}
}
Is this a good idea? Should I use setter methods in the constructor? Isn’t it a problem if I would override either the constructor or the setter methods in a sub class?
It’s probably not a good idea. If you don’t make that class final and don’t make the setName( … ) method private or final someone else is able to extend your class and overrid the setName( … ) method. Your constructor (in your base class) will call that method in the extending class instead of your implementation. Nobody knows what that method can do. As a rule of thumb: a constructor shouldn’t call methods that can be overriden.