Having trouble with inheriting constructor from superclass
I'm fairly new to inheritance so I'm not really sure what I'm doing wrong. The class I've written, "SoldCar" is supposed to represent a car that has been sold (obviously) and extends the "Car" class I have written. At line 18 I get an error saying
SoldCar.java:18: cannot find symbol
symbol : constructor Car(int)
location: class Car
super(id);
However, I do not get this message for the constructor above, which is written the same way. (I think). If anyone can take a look at my class and can figure out what is wrong with it, I would greatly appreciate it. Btw, if you get a chance, I would also appreciate if someone looked at my equals method to make sure I am comparing the Strings and Date objects correctly, thanks.
Code:
public class SoldCar extends Car
{
private double price;
private String customer;
private Date dateSold;
public SoldCar(double cost, int id, Date date, int year,
String make, double p, String cust, Date sold)
{
super(cost, id, date, year, make);
price = p;
customer = cust;
dateSold = sold;
}
public SoldCar(int id, double p, String cust, Date sold)
{
super(id);
price = p;
customer = cust;
dateSold = sold;
}
public double getDealerCost()
{
return dealerCost;
}
public int getIdNumber()
{
return idNumber;
}
public Date getDateArrived()
{
return dateArrived;
}
public int getModelYear()
{
return modelYear;
}
public String getMakeModel()
{
return makeModel;
}
public double getPrice()
{
return price;
}
public String getCustomer()
{
return customer;
}
public Date getDateSold()
{
return dateSold;
}
public double CalculateProfit(SoldCar car)
{
double profit = car.getPrice()-car.getDealerCost();
return profit;
}
public String toString()
{
return "Cost: "+dealerCost+"\tID: "+idNumber+"\tDate: "+dateArrived
+"\tYear: "+modelYear+"\tMake/Model: "+makeModel+"\tPrice: "
+price+"\tCustomer: "+customer+"\tDate Sold: "+dateSold;
}
public boolean isEqual(int year, Date date, String make, Date sold)
{
return modelYear==year && dateArrived==date && makeModel.equals(make)
&& dateSold==sold;
}
}
public class Car
{
protected double dealerCost;
protected int idNumber;
protected Date dateArrived;
protected int modelYear;
protected String makeModel;
public Car(double cost, int id, Date date, int year, String make)
{
dealerCost = cost;
idNumber = id;
dateArrived = date;
modelYear = year;
makeModel = make;
}
public double getDealerCost()
{
return dealerCost;
}
public int getIdNumber()
{
return idNumber;
}
public Date getDateArrived()
{
return dateArrived;
}
public int getModelYear()
{
return modelYear;
}
public String getMakeModel()
{
return makeModel;
}
public String toString()
{
return "Cost: "+dealerCost+"\tID: "+idNumber+"\tDate: "+dateArrived
+"\tYear: "+modelYear+"\tMake/Model: "+makeModel;
}
public boolean isEqual(int year, Date date, String make)
{
return modelYear==year && dateArrived==date && makeModel.equals(make);
}
}
Re: Having trouble with inheriting constructor from superclass
The error message says it all.
That line of code is trying to call a constructor in the parent class that only has one parameter (an int) and it doesn't exist.
Re: Having trouble with inheriting constructor from superclass
Ah, I see, I was assuming that super would only inherit the arguments that were specified in the braces from the super constructor. Thanks
Re: Having trouble with inheriting constructor from superclass
Using super is the same as any other constructor or method call, the type, order and number of parameters must match.