-1

I am making a chess game and tried using an inheritance desing for all the pieces. I want to make the subclass pawn use the method printPiece from Piece, but I have to modify the parameters on each piece. I know it's a bit silly to have the same parameters on both classes, but I don't know how to make the superclass use the print method without declaring icon in it. Same if i want to create getters on the superclass, the subclasses don't recognize them.

I have tried the code provided and it doesn't recognize that icon is 'i' from a pawn, it just uses the superclass method and writes null. Also tried making the parameters protected but same result.

public abstract class Piece{
    private String name;
    private int value;
    private boolean alive;
    private char icon;

    public Piece() {}

    public abstract Coordinate movePiece(Coordinate coor);

    public abstract boolean canMovePiece();

    public void printPiece()
    {
        System.out.print(icon);
    }
}

public class Pawn extends Piece {
    private String name;
    private int value;
    private boolean white;
    private char icon;

    public Pawn(boolean pWhite) {
        super();
        this.value = 1;
        this.name = "Pawn";
        this.white = pWhite;
        if(pWhite) {this.icon = 'I';}
        else {this.icon = 'i';}
    }

    public Coordinate movePiece(Coordinate coor){}

    public boolean canMovePiece() {}
}
  • What's wrong with your current code? Each piece should have its own "icon" char, no? – Hovercraft Full Of Eels Nov 02 '19 at 23:57
  • 2
    Possible duplicate of [What is the difference between public, protected, package-private and private in Java?](https://stackoverflow.com/questions/215497/what-is-the-difference-between-public-protected-package-private-and-private-in) – tevemadar Nov 03 '19 at 00:01
  • @HovercraftFullOfEels yes, but when I print a square of the chess board that has a pawn in it (matrix[1][0] = new Pawn(true) , the matrix is comprised of Piece objects, then i select which ones to convert to pieces) and matrix[1][0].printPiece() it doesn't print anything – Sergio Cortés Montero Nov 03 '19 at 00:04

2 Answers2

1

When you extend Piece, you want to inherit the things in Piece that are common to all subclasses of Piece. So you don't re-declare variables in Pawn.

            public class Piece
            {
                private char icon;
                public char getIcon() { return icon; }
                public void setIcon(char i) { icon = i; }
                public Piece(char c) { setIcon(c); }
            }

            public class Pawn extends Piece
            {
                public Pawn()
                {
                    super('I');
                }
            }

This is a way to have every subclass have an icon, stored in Piece, and ways to set it and access it.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • Thanks a lot! That did work. Just one last question, i am using a boolean to decide if a pawn is white or black and i pass it when creating a pawn, how can i add that to the constructor of pawn if i can only have 1 super('I') statement and not an if(white) then super('I') else super('i')? – Sergio Cortés Montero Nov 03 '19 at 00:27
  • Nevermind, just solved it, i put the if statement in the super constructor and used Character.toLowerCase(c). Thanks for the help mate! – Sergio Cortés Montero Nov 03 '19 at 00:41
0

By declaring a private char icon in the super class, you prevent the subclass from accessing it.

Instead of re-declaring a private char icon in the subclass, which is shadowing the parent's variable, you can declare a setter, or make it part of the parent's constructor.

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • But i want to be able to create a pawn without passing parameters, if i put it in the parent's constructor i will have to pass no? Also, i assume the setter is in the subclass, but how can i initialize a pawn object without having to execute the setter? Like matrix[1][0] = new Pawn(true) wouldn't do the setter would it? – Sergio Cortés Montero Nov 03 '19 at 00:12
  • the setter is of course in the parent class, since the field is private – njzk2 Nov 03 '19 at 00:44