I am new to java and am trying to build the logic to run a chess game. I have a superclass called ‘Piece’ and subclasses for king, knight, queen etc. I am trying to implement a move method where I determine the type of piece on the fly and then call that piece’s corresponding move method. For example:
int typeOfPiece = _board[startX][startY]._theKind;
Piece myPiece;
switch(typeOfPiece)
{
case 1:
myPiece = new Pawn(startX, startY, team);
case 2:
myPiece = new Rook(startX, startY, team);
case 3:
myPiece = new Knight(startX, startY, team);
}
boolean myPiece.canMove(endX, endY);
Is there a way that I can ensure the canMove method will be called by the correct type of piece in this example?
Thanks
Override
canMovein all subclasses and make it abstract in the parent classPiece.