So, let's say I have a class A, which is defined as follows:
public class A{
private int x;
public A(){
this(13);
}
public A(int x){
this.x = x;
}
}
And then I have a class B that needs to implement y, like this:
public class B extends A{
private int y;
public B(){
}
public B(int x){
this(x, 0);
}
public B(int x, int y){
super(x);
this.y = y;
}
}
My question is: what should I do in public B()? I call super () so that A assigns a default value / does everything and then does another this.y = yin this constructor, or am I calling this(13)?
Both approaches seem to require some bad methods. I write one of them this.y = yin two places. Another requires me to repeat the default value (s), and it will need to be changed every time the default value changes.
source
share