Results 1 to 4 of 4
  1. #1
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Post 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.

    Java 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);
    	}
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Having trouble with inheriting constructor from superclass

    The error message says it all.
    Java Code:
    super(id);
    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.

  3. #3
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default 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

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default 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.

Similar Threads

  1. Replies: 3
    Last Post: 03-29-2011, 08:29 AM
  2. Inheriting two kinds of Lists into one Class
    By rsiddharth in forum New To Java
    Replies: 22
    Last Post: 12-26-2010, 03:39 PM
  3. y superclass constructor is called automatically
    By _ShivamKapoOr_ in forum New To Java
    Replies: 1
    Last Post: 09-24-2010, 01:58 PM
  4. How is this dog inheriting a bark?
    By fresh83 in forum New To Java
    Replies: 3
    Last Post: 07-05-2010, 01:33 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •