Is there a way to "program to interface" in scala?
I am new in Scala. I have a "trait" in scala and many classes extending that trait. I am looking for a way on how to use the interface instead of the class directly.
Currently, I am doing :
val clazz = new MyClass()
where MyClass is extended from a trait.
I am looking for a way to return the interface as an instance from the factory mathods for generating classes. Is there a way?
MyClassimplements a traitFoobarand your method has return typeFoobar, you can returnclazzjust fine.asInstanceOf[MyClass]breaks type safety and defeats the purpose of "program to interface"asInstanceOf, which is the exact opposite of programming to an interface.