ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  10 Reasons We Need Java 3.0
Subject:   Interface naming collisions
Date:   2002-08-06 10:40:32
From:   wmshub
Response to: Interface naming collisions

But there's already a solution! Just use an inner class instead. I almost never implement interfaces in my "main" classes, so instead of:


public class Foo
implements Bar, Baz {


public void close() { /* Is this Bar.close or Baz.close? Oh no! */
}
}


you can write:


public class Foo {
public final Bar bar = new Bar() {
public void close() {
barClose();
}
}


public final Baz baz = new Baz() {
public void close() {
bazClose();
}
}


private void barClose() { ... }
private void bazClose() { ... }
}


Problem solved! Now just use foo.bar when you need the Bar interface and foo.baz when you need the Baz interface.


My only complaint is that it's a very verbose mneumonic. I'd prefer "thin" classes, like:
public thin Bar bar = Bar() { close=barClose };
to say "Bar is an inner bar object, but its close is equal to the bazClose in the outer object". Much easier to read.