Abstract classes with constructor parameters in Scala
scala
Solution
Your abstract class `Tile` declares a `val coordinate`, making this value publicly accessible. Your case class `EmptyTile` implicitly declares `coordinate` as a val as well (case class "magic"). Basically, your case class is effectively trying to override a value already provided by your abstract class.
You can either remove the `val` in your abstract class declaration, or not make `EmptyTile` and `OccupiedTile` case classes.
Edit: proposed alternative after comment:
trait Tile {
def coordinate: Int
def isOccupied: Boolean
def isEmpty() : Boolean = !isOccupied
def getPiece() : Option[Piece]
}
case class EmptyTile(coordinate: Int) extends Tile {
override def toString: String = "" +coordinate
val isOccupied = false
def getPiece() = None
}
case class OccupiedTile(coordinate: Int, val piece: Piece) extends Tile {
override def toString = piece.toString
val isOccupied = true
def getPiece = Some(piece)
}
Problem
I have the following class model: ``` sealed abstract class Tile(val coordinate: Int, val isOccupied: Boolean) { def isEmpty() : Boolean def getPiece() : Option[Piece] } case class EmptyTile(coordinate: Int) extends Tile(coordinate, false) { override def toString: String = "" +coordinate override def isEmpty() = true override def getPiece() = None } case class OccupiedTile(coordinate: Int, val piece: Piece) extends Tile(coordinate, true) { override def toString = piece.toString override def isEmpty = false override def getPiece = Some(piece) } ``` and I get the following error: ``` Error:(6, 22) overriding value coordinate in class Tile of type Int; value coordinate needs `override' modifier case class EmptyTile(coordinate: Int) extends Tile(coordinate, false) { ^ ``` What am I doing wrong? EDIT: Request to see Piece class, adding here: ``` import Alliance.Alliance import PieceType.PieceType abstract class Piece(val piecePosition: Int, val pieceType : PieceType, val alliance: Alliance) extends Movable { } object PieceType extends Enumeration { type PieceType = Value val PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING = Value } ```