Cannot instantiate the type for class object (Java

2020-01-29 02:17发布

Here's the code where I'm receiving the error (at the second "Killer" after "new").

String[] classes = new String[5];

kills[0] = "Brian Moser";
kills[1] = "James Doakes";
kills[2] = "Lila Tourney";
kills[3] = "Miguel Prado";

Killer morgan = new Killer("Dexter", 
                   "Morgan", 
                   kills,
                   4.0,
                   "Arthur Mitchell",
                   3,
                   false);

This has been giving me quite a bit of trouble, as I see no reason why this declaration should work based on my constructor for Killer.

And here's the Killer class:

import java.util.*;

public abstract class Killer{

public String firstName; 
public String lastName;
private String[] killList;
private double score;
private String nemesis;
private int accidents;
public boolean caught;

public Killer(String firstName, 
            String lastName, 
            String[] killList, 
            double score,
            String nemesis, 
            int accidents, 
            boolean caught) 
{
                this.firstName = firstName;
                this.lastName = lastName;
                for(int i = 0; i < 5; i++)
                    this.killList[i] = killList[i];
                this.score = score;
                this.nemesis = nemesis;
                this.accidents = accidents;
                this.caught = caught;
} //end constructor

I know this probably has a simple solution, but as of now, I'm not seeing it.

2条回答
不美不萌又怎样
2楼-- · 2020-01-29 02:29

following are few main points about abstract classes

  1. An abstract class is a class that is declared abstract.
  2. It may or may not include abstract methods.
  3. Abstract classes cannot be instantiated, but they can be subclassed
  4. Abstract classes does not contains any constructor

in your case there is a abstract class declared as public abstract class Killer so as its declaration defines that it is public, abstract class with named as Killer so as it is stated earlier that an Abstract classes cannot be instantiated so you need to subclass it or remove abstract keyword in order to get its instance for further read oracle docs abstract classes

查看更多
家丑人穷心不美
3楼-- · 2020-01-29 02:31

We can't instantiate abstract class. It's characteristics of abstract class. Here it's abstract so.

查看更多
登录 后发表回答