if you define a method inside interface, this method is always implicitly public.
In the class implementing this interface you must always declare this method as public,
otherwise compiler complains that this method is not implemented.

And the other compiler errors is not because of implementing interfaces, but due to
rules of overloading method (overloading = declare two/more methods with the same name in the class).
If you overload method, methods with the same name have to have different signatures (different parameters list).
You cannot declare inside class two methods with the same name and the same parameter list, compiler will always complain.

Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198

posted Jun 10, 2009 23:32:39

0

Ireneusz Kordal wrote:Hi,

if you define a method inside interface, this method is always implicitly public.
In the class implementing this interface you must always declare this method as public,
otherwise compiler complains that this method is not implemented.

thanks, updated the code.

Ireneusz Kordal wrote:Hi,
And the other compiler errors is not because of implementing interfaces, but due to
rules of overloading method (overloading = declare two/more methods with the same name in the class).
If you overload method, methods with the same name have to have different signatures (different parameters list).
You cannot declare inside class two methods with the same name and the same parameter list, compiler will always complain.

I know that. But my only question is, is there a way to implement these two interfaces simultaneously?

if you still would like to "implement" the two interfaces simultaneously you could use a workaround.

Have two other classes implementing each one of the interfaces and put objects of these classes as members inside an object of your original class. The you can hide the members as private and declare two public methods (which you have to give different names of course) and these public methods can delegate to the member object's methods. Alternatively you could implement the interfaces directly as a member variable with anonymous classes.

That way you avoid the name clash by implementing the interfaces in two different classes. But to the outside the functionality appears in only one class which delegates internally to the said classes.

Marco

Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198

posted Jun 11, 2009 04:51:28

0

Marco Ehrentreich wrote:Hi Aakash,

if you still would like to "implement" the two interfaces simultaneously you could use a workaround.

Have two other classes implementing each one of the interfaces and put objects of these classes as members inside an object of your original class. The you can hide the members as private and declare two public methods (which you have to give different names of course) and these public methods can delegate to the member object's methods. Alternatively you could implement the interfaces directly as a member variable with anonymous classes.

That way you avoid the name clash by implementing the interfaces in two different classes. But to the outside the functionality appears in only one class which delegates internally to the said classes.

Have two other classes implementing each one of the interfaces and put objects of these classes as members inside an object of your original class. The you can hide the members as private and declare two public methods (which you have to give different names of course) and these public methods can delegate to the member object's methods.

For example:
- class A implements interface I1
- class B implements interface I2
- class C has 2 members which refer to instances of A and B.

Normally if a class implements an interface the class can be used polymorphically. (class X isA interface Y)
But in your workaround there is only a HAS-A relationship.

So, in which cases do you use your workaround? Can you give an example?

of course you're right. This approach has some limitations. Because you're using composition with my proposed solution you can't refer to an object of class C with a variable or parameter of either type A or B.

So here's a solution which comes to mind. In any case you have to rename at least one of the delegating methods in class C to avoid the said name clash. So let's say on class C I have the methods

and

which are internally delegating to

respectively

which are both defined in the corresponding interfaces A and B.

If you still wanted to use an interface variable instead of a concrete class type to refer to an object of class C you could refactor both delegating methods into another interface D like this:

and implement this new interface in class C. Now you can us the interface when referring to an instance of class C like this:

But to be honest, how many times did you get in trouble like this in real applications? I've only seen this in artificially constructed scenarios like this. If you use good OO design principles and in particular follow the single responsibility principle (SRP) a class won't implement too many interfaces which cause name clashes anyway. So I really wouldn't care too much about this problem because in my opinion it's very unlikely to happen in a well-designed application.

As I said, don't worry too much about such issues in real world applications. Of course it's good to know that the compiler will complain (in particular for the exam!) in these situations though. But as far as I know you don't have to come up with ideas how to work around such limitations.

Are you sure about this. There's no error in this case as both the a methods in the two interfaces have the same signature and return type so the overriding a in class clash will override the a method in both A and B...

sorry for confusing anybody here, of course you're right. I should have been more careful with my examples. ........

But the the basic idea behind my examples how to solve such a naming conflict still should work to more or less elegantly implement multiple interfaces with conflicting methods.

I didn't had any confusion with your way of implementing multiple interfaces. I quoted from the original post where Aakash said that it will not compile, so I just wanted to clarify that it will compile fine. I'm fine with the solution that you gave for solving this naming conflict...

Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198

posted Jun 12, 2009 05:08:19

0

Ankit Garg wrote:

Are you sure about this. There's no error in this case as both the a methods in the two interfaces have the same signature and return type so the overriding a in class clash will override the a method in both A and B...