/ Published in: Java

Traits both provide a set of methods that implement behaviour to a class and require that the class implement a set of methods that parameterize the provided behaviour. A trait is kind of a “micro interface†that describes some characteristic of a class design that can be found in many different components throughout the system. By referring to the traits instead of the implementing class itself you can keep the system decoupled and modular.
Expand |
Embed | Plain Text
public interface HasComments<R extends HasComments<R>> { // one method that parameterize the provided behaviour List<Comment> getComments(); // two methods that implement the behaviour default R add(Comment comment) { getComments().add(comment); return (R) this; } default R remove(Comment comment) { getComments().remove(comment); return (R) this; } }
You need to login to post a comment.