Creation of object using abstraction fails, likely a simple fix I cannot see

26 views Asked by At

A novice question about abstraction and constructors. I feel like I'm missing something obvious.

I have an abstract class Piece, this is the constructor:

public abstract class Piece {

    private int[] location = new int[2];
    private final char color;

    public Piece(char color, int[] location) {
        this.location = location;
        this.color = color;
    }
}

I have a class which extends Piece:

public class Bishop extends Piece{

    public Bishop(char color, int[] location) {
        super(color, location);
    }
}

And I am trying to test it. However, the following code gives the errors illegal start of expression, illegal start of type, missing ';'

public class testing {
    Piece blackBishop = new Bishop('b', {0,0});
}

All these files are in the same package. After searching for a solution for hours I have decided to ask for help. Please let me know what I have done incorrectly.

1

There are 1 answers

1
Eran On BEST ANSWER

{0,0} can only be used by itself when you declare an int array variable (int[] var = {0,0};).

Change

Piece blackBishop = new Bishop('b', {0,0});

to

Piece blackBishop = new Bishop('b', new int[] {0,0});