Composition over inheritance is an old trend or even accepted state of the art in object oriented programming. It would be even easier to use in Java, if there were language support for delegation. Our IDEs, like eclipse, allow for easy generation of delegating methods, but the result is terrible code clutter. I would rather like to see something like
class ExtendedList<T> implements List<T> {
List<T> values = new ArrayList<T>();
delegate values.*;
...
}
where delegate
is a new language keyword that is nothing but a shortcut for what the IDE does explicitly when you ask it to delegate to all methods of the field values
.
I could go on to compare this with inheritance and how the result could be much the same, given interfaces, but more explicit and allowing fine grained, explicit "multiple inheritance", but this discussion must have happened already sometime in the past in some Java mailing list. Yet I could only find 15 year old links, for example on beust.com and citeseer describing ideas very similar to what I have in mind.
I think this is (a) really useful and (b) trivial to implement in a compiler, yet in 15 years nothing has appeared in Java.
So my question is: What am I missing? Is there a major hurdle in implementing this that I am not aware of?
As an aside: I would love to get good pointers to, for example, Java/JCP/JSR/whatever where this was discussed. I did not manage to find some.
As requested, let me try another example:
class FileWithMetas extends OutputStream implements Map<String,String> {
final OutputStream out;
delegate out.*; // takes care of "extend OutputStream"
final Map<String,String> metas = new HashMap<>();
delegate metas.*; // takes care of "implements Map"
public FileWithMetas(...) {
this.out = new FileOutputStream(...);
}
@Override //explicitly re-implement the delegated out.close()
public void close() {
//serialize the metas, for example
out.close(); // with classic inheritance this would be super.close()
}
}
This will look very much like multiple inheritance in the end, but as mentioned: the delegation is just syntactic sugar replacing delegation methods generated by the IDE and except for slightly different error messages, the compiler should behave exactly like that. In fact I think this could replace inheritance completely (not that I am asking for it) --- given we keep interfaces, of course, which allow us to separate types from implementation.