Interfaces

I'd be far more willing to use more ubiquitous interfaces in Java if I could do something akin to the following:

public interface SomeInterface {
    public void foo();
 
    public void bar();
}
public class SomeInterfaceImpl implements SomeInterface {
    public void foo() {
        System.out.println("Hello, world from SomeInterfaceImpl!");
    }
 
    public void bar() {
        System.out.println("Hello, world from SomeInterfaceImpl!");
    }
}
public class SomeClass implements SomeInterface {
 
    // Delegate all calls to foo() here.
    private SomeInterface someInterfaceInstance implements SomeInterface;
 
    public SomeClass(SomeInterface someInterfaceInstance) {
        this.someInterfaceInstance = someInterfaceInstance;
    }
 
    @Override
    public void bar() {
        System.out.println("Hello, world from SomeClass!");
    }
 
    public static void main(String... args) {
        SomeInterface someInterfaceImpl = new SomeInterfaceImpl();
        SomeClass someClassInstance = new SomeClass(someInterfaceImpl);
 
        someClassInstance.foo();
        someClassInstance.bar();
    }
}
> java SomeClass
> Hello, world from SomeInterfaceImpl!
> Hello, world from SomeClass!

This keeps you from having to type out this method on SomeClass:
@Override
public void foo() {
    this.someInterfaceInstance.foo();
}

Which is hella annoying. And yeah, you could get your IDE to do that for you, but then it adds clutter to your source code when you or somebody new comes to look at it later. Besides, the less typing/clicking there is, the more productive I can be! Plus, this promotes composition over inheritance, a lofty goal in software engineering. Win win, don't you think?
page_revision: 0, last_edited: 1252948319|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License