Results 1 to 3 of 3
- 03-07-2011, 03:20 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 38
- Rep Power
- 0
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;*/ } }
- 03-07-2011, 03:26 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 03-07-2011, 03:34 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
abstract classes
By renju krishnan in forum New To JavaReplies: 1Last Post: 09-29-2010, 08:31 AM -
question about abstract classes
By TheFlying_Boy in forum New To JavaReplies: 6Last Post: 07-08-2009, 07:19 AM -
interface vs abstract classes
By rosh72851 in forum New To JavaReplies: 7Last Post: 11-16-2008, 08:22 PM -
Inverfaces vs Abstract Classes
By ravian in forum New To JavaReplies: 1Last Post: 11-28-2007, 09:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks