BenJamin MacNeil
BenJamin MacNeil

Reputation: 15

Java: Accessing Subclass Methods from a Array of Parent objects

I'm trying to create a program that starts with a Piece class. For the purpose of the exercise every other class extends Piece. The other classes contain methods for moving the pieces, either one space or n spaces.

All of the pieces are stored in a 2D array which the use to move around.

My problem is that if i make an array of Pieces I cannot access the move methods as they are stored in the subclasses. I also can't just cast the object because i have 4 different types that the user can ask to move.

This is the code that adds a piece to the board

//adds a piece based on given type, but only if the space is clear (null)
public void addpiece(String type, String n, String c, int x, int y){
    if(board[x][y] == null){
        if(type == "FastFlexible"){
            board[x][y] = new FastFlexiblePiece(n,c,x,y);
        }
        else if(type == "FastPiece"){
            board[x][y] = new FastPiece(n,c,x,y);
        }
        else if(type == "SlowFlexible"){
            board[x][y] = new SlowFlexiblePiece(n,c,x,y);
        }
        else if(type == "SlowPiece"){
            board[x][y] = new SlowPiece(n,c,x,y);
        }
        else{
            System.out.println("Invaild type");
        }
    }
}

And this is the code that tries to move the piece, the error I get is because the parent Piece doesn't have a move method, but I can't figure out a way to get the pieces to cast correctly

 //Move a piece, two method one for fast and one for slow
public void movePiece(int x, int y, String direction){
    if(board[x][y] != null){
        if(board[x][y].getType().equals("SlowPiece")){
            board[x][y] = board[x][y].move(direction);
        }
        else if(board[x][y].getType().equals("SlowFlexible")){
            board[x][y] = board[x][y].move(direction);
        }
    }
}

There is another similar method for fast pieces.

Constructor for the slowPiece:

//Constructor
public SlowPiece(String n, String c, int x, int y){
    super(n,c,x,y);
    this.setType("SlowPiece");
}

But the code doesn't notice what type any of the Pieces are and so I can't cast them correctly

Upvotes: 0

Views: 416

Answers (2)

deepak marathe
deepak marathe

Reputation: 408

The very aim of Polymorphism is to avoid writing code like the implementation specified for public void movePiece(int x, int y, String direction){.

board[x][y] can refer to SuperType Piece and any of its SubTypes like the SlowPiece, SlowFlexible, FastPiece, FastFlexible. Piece can have the abstract move behavior specified in the definition of the class, without having to provide the implementation. All the SubTypes of Piece class provide their own implementation for move method.

The method public void movePiece(int x, int y, String direction) would simply boil down to this :

    public void movePiece(int x, int y, String direction){ 
        board[x][y].move(direction);
    }

At runtime, move method is dynamically dispatched depending upon the SubType of Piece class.

Upvotes: 1

Michael
Michael

Reputation: 2773

My suggestion is to add an abstract method to the parent Piece class.

public class Piece{
    public abstract void move();
}

NOTE: now you can't directly instantiate a Piece. This code is illegal:

Piece p = new Piece();

Upvotes: 0

Related Questions