Reputation: 5
I have a class Accommodation
as follows:
class Accommodation {
int accommodationNo
String address1
String address2
String postcode
double valuation
double rent
int noRentPaymentsPerYe
}
Class Housing
extends Accommodation
:
class Housing : Accomodation {
String type
int noBedrooms
int noCarParkingSites
}
And class Flat
extends Housing
:
class Flat : Housing {
double maintenanceCharge
}
When creating a constructor with parameters of Flat
class, do I include every field up to Accommodation
? An example of a constructor with parameters of class Flat
would be highly appreciated.
Upvotes: 0
Views: 38
Reputation: 16259
Yes, you have to include every field, and pass that down the chain of constructors (this is an abbreviation of the fields since only a couple per class is needed to demonstrate - you would need to add your additional fields):
public abstract class Accommodation {
private int accommodationNo;
private String address1;
protected Accomodation(int acomNo, string addr1) {
this.accomodationNo = acomNo;
this.address1 = addr1;
}
}
public class Housing {
private String type;
private int noBedrooms;
public Housing(int acomNo, string addr1, string typ, int noBeds)
: base(acomNo, addr1) {
this.type = typ;
this.noBedrooms = noBeds;
}
}
public class Flat {
double maintenanceCharge;
public Flat(int acomNo, string addr1, string typ, int noBeds, double maintCharge)
: base(acomNo, addr1, typ, noBeds) {
this.maintenanceCharge = maintCharge;
}
}
Upvotes: 1