The strange case of false interfaces

From AWiki

Jump to: navigation, search
public interface InterfaceX {
	public abstract void aMethodX();
}

public interface InterfaceY {
	public abstract String aMethodY(final String s);
}
public class FatImplementation implements InterfaceX, InterfaceY {
	public void aMethodX() {
		...
	}

	public String aMethodY(final String s) {
		...
		return ...;
	}
}

And finally... the crazy class, whose method is casting to InterfaceX an InterfaceY reference, relying on the fact that FatImplementation implements both interfaces.

public class CrazyClass {
	public void theCrazyCastingMethod(final InterfaceY y) {
		...
		((InterfaceX)y).aMethodX();
	}
}

This is a false use of abstraction, since the cast is succesful only with a specific implementation of the interfaces. Poor and silly design, don't you think?

Personal tools