I have two classes : Year and LeapYear setting the days of the year to 365 and 366 respectively within their constructors. I want to achieve something specific here.i want to override the Year’s constructor in the LeapYear’s constructor to set the number of days to 366? Alternatively i have done the following.
public class Year {
private int days;
public Year() {
this.days = 365;
}
public void setDays(int days) {
this.days = days;
}
public int getDays() {
return this.days;
}
}
public class LeapYear extends Year {
public LeapYear() {
setDays(366);
}
}
The model is wrong and that’s why you have trouble with the constructors: it’s simply not true that a 366-days-leap-year is-a 365-days-year. But that’s what you model tries to tell us.
You better model it like that: