I got this error message when I try to init a new object.
Cannot instantiate the type CarMy code
Main.java
public class Main { public static void main(String args[]){ Car car = new Car(4,4,COUNTRY.MALAYSIA, Locale.ENGLISH, "150.00"); //error here }
}Car.java
public abstract class Car implements Automobile { public int wheel; public int door; public COUNTRY country; public Locale locale; public String price; public Car(int w, int d, COUNTRY c, Locale l, String p){ this.wheel = w; this.door = d; this.country = c; this.locale = l; this.price = p; }
} 2 3 Answers
Car is an Abstract class you cannot create an instance of it.
public abstract class Car implements Automobileyou can potentially do something like
public class FordFocus extends Car keep in mind that you will need to call the super constructor, but then you will be able to create an instance of the FordFocus car type
Your class Car is an abstract class and you cannot create an instance of an abstract class.
Solution 1
Instead you need to create a concrete class that extends your class Car and then you can create an instance of that concrete class.
Solution 2
Remove abstract from your Car class declaration (But I think you don't want to do that).
Documentation states that
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
So reference Car car is supported, but object new Car(); is not supported.