I'm currently learning Scala and haven't programmed much the last few years, so I may have forgot some OOP fundamentals.
I'm trying to implement the two classes:
case class Rectangle(a : Int,b : Int) extends Shape case class Square(a : Int) extends Shape The trait/abstract class (I tried both, neither works) Shape is defined as follows:
trait Shape { def a : Double def b : Double def area = a *b def sides = 4 def perimeter = 2 * (a+b) def equals(r: Shape){ r.a == this.a && r.b == this.b } } The desired behavior is Rectangle(2,2) == Square(2)
I thought it should work like this as the operator == should use the equals method which I expect to use the most specific implementation (default implementation when called with some random class and my implementation when called with another Shape as argument.
So, my questions are:
- How could this be implemented?
- Is there a reason this should not be implemented (e.g. it is in conflict with some OOP principles, though it would be transitive, reflexive and symmetric)
- Should Shape be an abstract class or a trait?