Computer Science Canada Traits and Interfaces |
Author: | wtd [ Thu Jan 11, 2007 5:25 am ] | ||||||||||||||||||
Post subject: | Traits and Interfaces | ||||||||||||||||||
Understanding traits, coming from Java interfaces. At the simplest level, you can roughly equate these two language constructs.
We can replicate the functionality of Java interfaces with traits.
Traits can extend other traits, as interfaces can in Java. But the real fun comes from the differences. An interface in Java may contain only specifications for methods, and not actual definitions. To get a mix, we have to use abstract classes, but we can't use those like interfaces. This gives rise to a pattern.
Here we have an interface Foo which defines that any object of type Foo will have a public "foo" method which returns a String. The static method "printFoo" in the FooUtilities class takes advantage of this to act on an object of type Foo. We can replicate this pattern in Scala easily enough.
This looks a bit cleaner. The thing is, though, we can do better. In the original Java, we used a class with a static method to abstract out concrete functionality that was solely dependent on the abstract functionality present in any object of type Foo. As Scala allows traits to include concrete functionality, this pattern is not necessary.
Alternatively, we can abstract this concrete functionality out, and only mix it into implementing classes where explicitly desired.
We can even choose to only mix the functionality in ExtendedFoo into specific objects.
Conclusion Understanding the similarities and differences between traits in Scala and interfaces in Java can lead to some pretty interesting insights about the abstractions present in the two languages, and the patterns that arise to deal with those abstractions. |