Reputation: 27
I'm trying my hand at inheritance for the first time and I'm having difficulty wrapping my head around this.
Basically I have a Customer class and a WarrantyCustomer class. WarrantyCustomer says that the customer has a warranty and should be given a discount.
My Customer looks like this (not the whole class)
public class Customer {
private int monthsSinceVisit;
private double totalVisitCost;
(public constructor is here)
public int getMonthsSinceVisit(){
return monthsSinceVisit;
}
public double getTotalVisitCost(){
return totalVisitCost;
}
public double getNextVisitCost(){
totalVisitCost = 20.00 + (12.00*monthsSinceVisit);
return totalVisitCost;
}
}
And then in WarrantyCustomer I have:
public class WarrantyCustomer extends Customer {
private int monthsSinceDiscount;
@Override
public int getMonthsSinceVisit(){
return super.getMonthsSinceVisit();
}
@Override
public double getTotalVisitCost(){
return super.getTotalVisitCost();
}
(public constructor is here)
@Override
public double getNextVisitCost(){
while (monthsSinceDiscount>=12){
totalVisitCost = ((20.00 + (12.00*monthsSinceVisit)) * 0.20);
monthsSinceDiscount = 0;
}
return totalVisitCost;
}
}
I am unsure of why in my getNextVisitCost() in WarrantyCustomer it tells me that totalVisitCost and monthsSinceVisit are private in Customer. I know they are private but I thought I brought them into this class with my getter methods.
Thank you for any help. For this I am not permitted to set my instance variable to public or protected.
Upvotes: 2
Views: 57
Reputation: 575
Since WarrantyCustomer
is already inheriting the getters and setters from Customer
and you don't need to modify them, you don't need to override them.
But the problem was you were trying to set the value of the totalVisitCost
directly, instead of creating a setter and using it in the child class.
public class Customer {
private int monthsSinceVisit;
private double totalVisitCost;
// (public constructor is here)
public int getMonthsSinceVisit(){
return monthsSinceVisit;
}
public double getTotalVisitCost(){
return totalVisitCost;
}
// public setter for the private totalVisitCost
public void setTotalVisitCost(double cost) {
totalVisitCost = cost;
}
public double getNextVisitCost(){
totalVisitCost = 20.00 + (12.00*monthsSinceVisit);
return totalVisitCost;
}
}
public class WarrantyCustomer extends Customer {
private int monthsSinceDiscount;
// Don't need these overrides, it's already inherited.
// @Override
// public int getMonthsSinceVisit(){
// return super.getMonthsSinceVisit();
// }
// @Override
// public double getTotalVisitCost(){
// return super.getTotalVisitCost();
// }
@Override
public double getNextVisitCost(){
while (monthsSinceDiscount>=12){
// use the inherited setter and getter
setTotalVisitCost(((20.00 + (12.00*getMonthsSinceVisit())) * 0.20));
monthsSinceDiscount = 0;
}
return getTotalVisitCost();
}
}
Upvotes: 0
Reputation: 3027
for this problem you should define a setter for your totalVisitCost
and then access it in subclass:
public class Customer {
private int monthsSinceVisit;
private double totalVisitCost;
public int getMonthsSinceVisit(){
return monthsSinceVisit;
}
public double getTotalVisitCost(){
return totalVisitCost;
}
protected void setTotalVisitCost(double totalVisitCost) {
this.totalVisitCost = totalVisitCost;
}
public double getNextVisitCost(){
totalVisitCost = 20.00 + (12.00*monthsSinceVisit);
return totalVisitCost;
}
}
public class WarrantyCustomer extends Customer {
private int monthsSinceDiscount;
@Override
public int getMonthsSinceVisit(){
return super.getMonthsSinceVisit();
}
@Override
public double getTotalVisitCost(){
return super.getTotalVisitCost();
}
@Override
public double getNextVisitCost(){
while (monthsSinceDiscount>=12){
setTotalVisitCost(((20.00 + (12.00*getMonthsSinceVisit())) * 0.20));
monthsSinceDiscount = 0;
}
return getTotalVisitCost();
}
}
Upvotes: 1