I am trying to make this Sub Class Circle inherit the traits of parent class Shapes. I want the Circle Class to accept a colour string but I keep getting an error.
abstract class Shape {
def getArea():Double
var colour = "Red"
def getColour():String = colour
def setColour(newColour:String) = {
colour = newColour
}
}
case class Circle (var radius:Int, override var colour:String) extends Shape {
override def getArea():Double = 3.14 * radius * radius
}
The error I get is:
"ScalaFiddle.scala:13: error: overriding field colour in class Shape of type lang.this.String;"
You can't override a var
but you can assign to it.
case class Circle (var radius:Int, clr:String) extends Shape {
colour = clr
...
BTW, mutable variables are dangerous. Just pretend that var
doesn't exist. You'll write better code without it.