This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Re: Interfaces Can Have Same Methods
Posted by J�rg Hettel on July 26, 2000 at 10:14 AM
> > I have a question: > > If you have 2 interfaces below: > > public interface A > > { > > public void sayHello(); > > public void sayWorld(); > > } > > public interface B > > { > > public void sayHello(); > > public void sayYahoo(); > > } > > Let a class C implement interfaces A , B: > > public class C implements A,B > > { > > public void sayHello(){ System.out.println("Hello"); //interface does this implementation belongs to ??? > > //Will it even compile??? > > public void sayWorld(){ System.out.println("World"); > > public void sayYahoo(){ System.out.println("Yahoo"); > > } > The sayHello() implementation belongs to both interfaces. In > other words, if you cast a reference to a class C object to > either A or B, you can invoke sayHello(). > bv One remark to Interfaces: There is on problem with interfaces if you model roles with the help of interfaces (as mention in several design books). Assume we have two different roles that comes from domain analysis. The architects will usually model this roles by interfaces. Objects that can play a role will implement such an interface. interface Role1 { public abstract void methode(); } interface Role2 { public abstract void methode(); } public class Entity implements Role1, Role2 { public void methode() { ... } // .... }
The problem is that Entity can only implement one role behavior. (in gerneral the behavior in Role1 is different to the behavior in Role2) The developer has to decide which role has to be ignored by implementing only one of the interfaces. So the name conflict of the methode leads to a inadequate design. Interfaces are a powerful design instrument but have also some limitations. J�rg
Replies:
|