1

I know that interfaces have abstract methods that are inherited by their implementations. I'm wondering if there is a way that I can declare a more concrete method that each implementation will have, and will act in exactly the same way for each implementation.

I have a chess piece interface as a blueprint for creating different pieces, and I want to add each newly instantiated piece to an ArrayList, no matter the specific implementation.

Here is my Piece interface:

public interface Piece {

    public enum Side {
        BLACK, WHITE
    }

    public void getSide();

    public void movePiece(Position position);

    public void takePiece(Position position);

    public void getPosition();
}

And here is my Board class, which contains a list of active pieces which I want to add each piece to when it is created.

import java.util.ArrayList;

public class Board {

    public ArrayList<Piece> activePieces;

}

Do I have to add a specific method to each implementation of Piece? Or is there some way I can include this in the interface? I feel like it's the former, but it just feels like there should be a neater way to do this.

0 Answers0