Abstract Classes??? Creating a new object????
I am having trouble creating new objects in abstract classes? Basically I am gettin a error - "Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot instantiate the type Car
Cannot instantiate the type Truck
Cannot instantiate the type Car
at Tester.main(Tester.java:6)"
Could someone help me out?
Thanks.
Code below.
Code:
public abstract class Vehicle {
private double regoCost;
private int regoNumber;
private int yearManufactured;
private int currentYear = 2010;
public Vehicle(int yearManufactured, int regoNumber)
{
this.yearManufactured = yearManufactured;
this.regoNumber = regoNumber;
}
public boolean regoCheck()
{
int vehicleAge = this.yearManufactured - currentYear;
if (vehicleAge > 4)
{
return true;
}
else
{
return false;
}
}
public void setRegoCost(double regoCost)
{
this.regoCost = regoCost;
}
public String toString()
{
return this.regoNumber +
" " +
this.regoCost +
" " +
this.yearManufactured +
" ";
}
public abstract int getRegoCost();
}
Code:
public abstract class Car extends Vehicle{
private String usageType;
public Car(int regoNumber, int yearManufactured, String usageType)
{
super(regoNumber, yearManufactured);
this.usageType = usageType;
}
public int getRegCost()
{
if (usageType.equals("Business"))
{
super.setRegoCost(50);
}
else if (usageType.equals("Private"))
{
super.setRegoCost(300);
}
return this.getRegoCost();
}
public String toString()
{
return super.toString() +
" " +
this.usageType;
}
}
Code:
public abstract class Truck extends Vehicle{
private int weight;
private Truck(int regoNumber, int yearManufactured, int weight)
{
super(regoNumber, yearManufactured);
this.weight = weight;
}
public int getRegCost()
{
if (weight < 5000)
{
super.setRegoCost(300);
}
else if (weight > 5000)
{
super.setRegoCost(400);
}
return this.getRegoCost();
}
public String toString()
{
return super.toString() +
" " +
this.weight;
}
}
Code:
public class Tester {
public static void main(String[]args)
{
Vehicle[] vehicles = new Vehicle[3];
Car porsche = new Car(12345, 2006, "Business");
Truck mac = new Truck(666777, 1985, 10000);
Vehicle v = new Car();
vehicles[0] = porsche;
vehicles[1] = mac;
vehicles[2] = v;
for (int i =0; i < vehicles.length; i++)
{
System.out.println(vehicles[i]);
}
/*Vehicle vp;
Car ca = new Car(12345, 2006, "Business");
vp = ca;*/
}
}