Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

Cannot instantiate the type for class object (Java)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...