Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 458
Michael Anthony Leber Avatar asked Oct 15 '25 04:10

Michael Anthony Leber


2 Answers

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

like image 123
Zahid Ali Avatar answered Oct 16 '25 17:10

Zahid Ali


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

like image 42
Jwalin Shah Avatar answered Oct 16 '25 18:10

Jwalin Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!