4

I got this error message when I try to init a new object.

Cannot instantiate the type Car

My 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;
    }

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Nurdin
  • 23,382
  • 43
  • 130
  • 308

3 Answers3

15

Car is an Abstract class you cannot create an instance of it.

public abstract class Car implements Automobile

you 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

David Gardener
  • 93
  • 2
  • 13
Kenneth Clark
  • 1,725
  • 2
  • 14
  • 26
4

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

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
0

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.

Naman Gala
  • 4,670
  • 1
  • 21
  • 55