Referencing method from abstract class

Meezy98

START OF INSTRUCTIONS

If there is a game piece on the clicked square (clickedSquare.getPiece()!= null) Make sure the game piece belongs to the current player. You can get the owning player by calling the getPlayerType() method on the AbstractGamePiece returned by getPiece(). You can then compare that to the currentPlayerTurn JailBreak class member.

If the piece on the clicked square belongs to the current player Set the selected square equal to the clicked square Call the select() method on the selected square to show the yellow border

END OF INSTRUCTIONS

I've figured out how to highlight the piece by implementing the select() method, butt I've tried several different implementations, such as AbstractGamePiece.getPlayerType()==currentPlayerTurn, using nested if statements to set conditions on clickedSquare.getPiece(), and a couple others that I can't think of off the top. I can't seem to get a reference to getPlayerType() from the abstract class. There is pre-written code in the class that seems to work fine as far as accessing the AbstractGamePiece, such as

private void changePlayerTurn()
    {
        if (currentPlayerTurn == AbstractGamePiece.PLAYER_OUTLAWS)
            currentPlayerTurn = AbstractGamePiece.PLAYER_POSSE;
        else
            currentPlayerTurn = AbstractGamePiece.PLAYER_OUTLAWS;
    }

I feel like I'm going about this wrong, but I can't seem to get a reference to the getPlayerType(). The one time I did, I created a new object of the abstract class, but the constructor needs 3 parameters that aren't really appropriate here.

Supporting code:

abstract public class AbstractGamePiece
{
    // All class members are provided as part of the activity starter!
    
    // These two constants define the Outlaws and Posse teams
    static public final int PLAYER_OUTLAWS = 0;
    static public final int PLAYER_POSSE = 1;

    // These variables hold the piece's column and row index
    protected int myCol;
    protected int myRow;
    
    // This variable indicates which team the piece belongs to
    protected int myPlayerType;
     
    // These two strings contain the piece's full name and first letter abbreviation
    private String myAbbreviation;
    private String myName;

    // All derived classes will need to implement this method
    abstract public boolean hasEscaped();
    
    // The student should complete this constructor by initializing the member
    // variables with the provided data.
    public AbstractGamePiece(String name, String abbreviation, int playerType)
    {
    myName = name;
    myAbbreviation = abbreviation;
    myPlayerType = playerType;
    }

    public int getPlayerType()
    {
        return myPlayerType;    
    }
    public void setPosition (int row, int col)
    {
     myRow = row;
     myCol = col;
    }
    public int getRow()
    {
        return myRow;
    }
    public int getCol()
    {
        return myCol;
    }
    public String getAbbreviation() 
    {
        return myAbbreviation;
    }
    public String toString()
    {
        return (myName + " at " + "(" + myRow + "," + myCol + ")");
        
    }
    public boolean canMoveToLocation(List<GameSquare> path)
    {
        return false;
    }
    public boolean isCaptured(GameBoard gameBoard)
    {
        return false;
    }

Code that highlights the pieces indiscriminately has been implemented successfully.

private void handleClickedSquare(GameSquare clickedSquare)
    {
        if (selectedSquare == null)
        {
            selectedSquare=clickedSquare;
            selectedSquare.select();
        }
        else if (selectedSquare == clickedSquare)
        {
        selectedSquare.deselect();
        selectedSquare = null;
        }
            
        else 
        {
        }

Why is it that I'm unable to create a reference to the getPlayerType() method?

rzwitserloot

Just call getPlayerType on any expression of type X, where X is either AbstractGamePiece or any subclass thereof. For example, square.getPiece().getPlayerType().

Method references are a thing in java and are definitely not what you want, you're using words ('I'm unable to create a reference to getPlayerType') that mean something else. A method reference looks like AbstractGamePiece::getPlayerType, and let you ship the concept of invoking that method around to other code (for example, you could make a method that calls some code 10 times in a row - so this method takes, as argument, 'some code' - and method references are a way to that). It is not what you want here, you want to just invoke that method. Which is done with ref.getPlayerType() where ref is an expression whose type is compatible with AbstractGamePiece. From context, clickedSquare.getPiece() is that expression.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Class that is extended from an abstract class toString method doesn't work :/

Referencing a method from a class which is nested

static method from abstract class

How to call abstract method from abstract class called by inherit class

Error "__init__ method from base class is not called" for an abstract class

Protected abstract or public abstract method in abstract class

Calling a abstract method from a callback inside a abstract class

method referencing for custom class method

Unable to call abstract class method from interface

How to create an instance of concrete class from static method of abstract class

Using an overridden method from an abstract class (Java)

Referencing a variable declared in abstract class from inherited class

Java - Calling method from child of abstract class

Referencing a class variable from another class via a method

Overriding Method with Generic Parameters from Abstract Class

Returning a class reference from the static method of an abstract class

Testing an abstract method of a child-class from an abstract class

Abstract Method In Abstract Class In java

Using a method from an abstract class with different parameters

Referencing a react component from its class method

Abstract method in a non abstract class

How to instantiate class from static method of abstract class

Referencing instance variables from within abstract class

Call a method from abstract class c#

Method not found in derived class inherited from abstract class

Override virtual method from abstract class

Kotlin concrete class extending from abstract class and interface, with the interface using a method implemented in the abstract class

Different method behavior in child class from abstract parent class

C# error when referencing a static method from another class