Results 1 to 3 of 3
  1. #1
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

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

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

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You cannot instantiate an abstract class.

    Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

    Hint: Since Car and Truck don't have any abstract methods, they probably don't need to be abstract classes.

    -Gary-

  3. #3
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default

    Oh ok. I actually had a typo in the getRegoCost() methods so eclipse was forcing me to create Truck and Car classes as abstract. Got it working now.... Thanks for the help Gary...

Similar Threads

  1. abstract classes
    By renju krishnan in forum New To Java
    Replies: 1
    Last Post: 09-29-2010, 08:31 AM
  2. question about abstract classes
    By TheFlying_Boy in forum New To Java
    Replies: 6
    Last Post: 07-08-2009, 07:19 AM
  3. interface vs abstract classes
    By rosh72851 in forum New To Java
    Replies: 7
    Last Post: 11-16-2008, 08:22 PM
  4. Inverfaces vs Abstract Classes
    By ravian in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 09:53 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
  •