I have read over the internet that Traits in Scala are
interfaces that can provide concrete members
this means, traits are interface and we may provide body to methods in interface. I have a simple query here, if that is possible than why my below code shows error:
Error:(6, 8) object Car inherits conflicting members: method startEngine in trait Vehical of type => Unit and method startEngine in trait Motor of type => Unit (Note: this can be resolved by declaring an override in object Car.) object Car extends Vehical with Motor {
trait Vehical {
def startEngine : Unit = {
println("Vehical Start")
}
def stopEngine
}
trait Motor {
def startEngine : Unit = {
println("Motor Start")
}
def stopEngine
}
object Car extends Vehical with Motor {
override def stopEngine : Unit = {
println("Stop Engine")
}
def main(args: Array[String]): Unit = {
Car.startEngine
Car.stopEngine
}
}
Being a java developer I would have not provided body inside interface but scala traits allowed this. If traits are not interface then I'll consider them as an abstract class, that means no interface allowed in scala.
Also please let me know how to resolve this ambiguity problem. How can I use my startEngine method in child class if the same method is available in multiple traits.