default methods in interfaces
Brian Goetz
brian.goetz at oracle.com
Tue Oct 29 12:53:01 PDT 2013
>> Default methods can be used as a limited form of traits to mix-in behaviour but not state, so they ain't like Scala's traits. Any state (or context) needs to be passed as parameters to those methods. Also note that because there is no state then there are no state-realted issues associated multiple
>
> Usually an interface with default methods is stateful, e.g. List; it's
> just a slightly extra work for subclass to provide the actual storage
> space. We may traits like interfaces for example
>
> interface Colored
> abstract Color getColor();
> abstract void setColor(Color color);
> default void trasformColor(...)...
We call this the "virtual field pattern". In C++ parlance, using this
pattern gets you the equivalent of virtual base classes.
Because interface methods must be public, though, this ends up leaking
your state to clients, which is sometimes OK but often not. Use sparingly.
More information about the lambda-dev
mailing list