-
Enhancement
-
Resolution: Duplicate
-
P4
-
None
-
1.3.0, 1.4.0
-
generic
-
generic
Name: rmT116609 Date: 11/13/2000
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
I would like the Java language (compiler front end) to support a syntactic
construct for compile-time delegation of implementation from a wrapper class to
a delegation class. I realize that there is now a way to interpose classes that
generically handle and delegate calls, but this mechanism is slow compared to
direct method invocation, and although the dynamic nature of it has very
interesting uses, a compiletime delegation mechanism would meet most needs for
delegation in the language and would perform much better with better
compile-time safety.
Inheritance in Java is often used to provide a default implementation of a
class, or an implementation of some arcane aspects of a class. The problem is
that classes can implement multiple interfaces, and potentially every interface
that a class implements might need a default implementation. C++ provides
multiple inheritance, which is often used to solve this problem, but that
introduces many implementation complications, e.g., polymorphism. Java wisely
avoided multiple inheritance.
Essentially, the default implementation that you get from inheritance is just
syntactic sugar for delegation. But inheritance also implies an is-a
relationship among the classes, that is overkill if all you want is a default
implementation.
Here is an example of what I'd like to see. The syntax might be varied slightly
but this is the idea of how a wrapper class foo would be implemented:
class foo delegates PropertyChangeSupport p {
public foo() {
p = new PropertyChangeSupport();
}
}
This is the same as defining an interface corresponding to the class named
PropertyChangeSupport, and writing the following for each method m in the
interface:
<returnType> m( <parametersOfM> ) { return p.m( <parametersOfM> ); }
The class foo can optionally override this default delegation method, if
desired. The delegate class (e.g., PropertyChangeSupport) can optionally
be an interface.
Aside from relieving the programmer of having to hand-write each delegation
method of PropertyChangeSupport, this mechanism also makes
code maintenance much easier. Consider that if a new method is added
to PropertyChangeSupport, it is no longer necessary to modify each and
every class that delegates to it to add the new method. Having done this many
times, I can attest to what a royal pain it is.
Writing a wrapper class now becomes trivial, since all you do is delegate to the
classes being wrapped, and implement the constructor to find the instances of
the delegated classes. Persistent objects could be implemented this way, for
example.
This is the same kind of syntactic sugar that inheritance provides, but it is
not tied to the other complex aspects of inheritance, such as polymorphism.
(Review ID: 112125)
======================================================================
- duplicates
-
JDK-4191243 Method forwarding - Subclassing without subtyping
-
- Closed
-