Favor Composition Over Inheritance

The mantra "Favor Composition over Inheritance" has, with good reason, been repeated many times in literature. However, there is little or no language support in Java to simplify the composition of objects. However, with a new JEP draft named "Concise Method Bodies," the situation might improve.

Brian Goetz is responsible for the JEP draft, which likely will be handled under project "Amber." The complete draft can be found here.

Concise Method Bodies

The JEP, when implemented, allows for something called Concise Method Bodies (CMB), whereby, loosely speaking, a method body can be a lambda or a method reference. Here is one example:

Old Style

int length(String s) { return s.length(); }





New CMB

int length(String s) -> s.length(); // -> is "single expression form"





Or, we can see the new CMB as shown below:

int length(String s) = String::length; // = is "method reference form"





This will reduce boilerplate coding while improving code readability.

Composition

Consider the existing Java class Collections.UnmodifiableList , which delegates an inner List class and prevents it from being modified (code shortened and reordered for readability):

static class UnmodifiableList<E> extends UnmodifiableCollection<E> implements List<E> { final List<? extends E> list; UnmodifiableList(List<? extends E> list) { super(list); this.list = list; } public boolean equals(Object o) {return o == this || list.equals(o);} public int hashCode() {return list.hashCode();} public E get(int index) {return list.get(index);} public int indexOf(Object o) {return list.indexOf(o);} public int lastIndexOf(Object o) {return list.lastIndexOf(o);} public E set(int index, E element) { throw new UnsupportedOperationException(); }





With CMB, it can be implemented like this:

static class UnmodifiableList<E> extends UnmodifiableCollection<E> implements List<E> { final List<? extends E> list; UnmodifiableList(List<? extends E> list) { super(list); this.list = list; } public boolean equals(Object o) = list::equals; public int hashCode() = list::hashCode; public E get(int index) = list::get; public int indexOf(Object o) = list::indexOf; public int lastIndexOf(Object o)= list::lastIndexOf; public E set(int index, E element) { throw new UnsupportedOperationException(); }





I think this feature makes a lot of sense. It is especially useful when delegating methods with one or several parameters. Hope you enjoyed!

EDIT: By using CBM also for the throwing methods (as exemplified in the last row below) the class could be reduced even further:

static class UnmodifiableList<E> extends UnmodifiableCollection<E> implements List<E> { final List<? extends E> list; UnmodifiableList(List<? extends E> list) { super(list); this.list = list; } public boolean equals(Object o) = list::equals; public int hashCode() = list::hashCode; public E get(int index) = list::get; public int indexOf(Object o) = list::indexOf; public int lastIndexOf(Object o) = list::lastIndexOf; public E set(int index, E element) = UnmodifiableList::thrower;







