aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes implementing two interfaces with the same method name! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "implementing two interfaces with the same method name!" Watch "implementing two interfaces with the same method name!" New topic
Author

implementing two interfaces with the same method name!

Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198


Compiler says : a() is already defined.

Is there some way of making clash implement both A and B?


Another scenario:


implementation1:

Compiler says: A.a() not defined.

implementation2:

Compiler says: a() is already defined.

Same question:
Is there some way of making clash implement both A and B?

SCJP 5 95%
SCJP FAQ | SCJP Mock Tests | SCJP Tipline | Generics
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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.

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
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?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

is there a way to implement these two interfaces simultaneously?


As pointed out earlier it's about two methods with same signature which is not allowed inside a class, that is if you are going to implement these two interfaces in the same class.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
Vijitha Kumara wrote:
As pointed out earlier...


When?

I got the point though. thanks.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

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.

Marco
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
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.

Marco


Thanks Marco, thats what I wanted to know.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

You're welcome!

I'm glad that it really helped you

Marco
Till Stoschus
Greenhorn

Joined: May 28, 2009
Posts: 14
Hi Marco,

i have a question concerning your 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.


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?

Thanks, Till
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Till,

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.

Btw. I hope the example is still readable with all those letters

Marco


Till Stoschus
Greenhorn

Joined: May 28, 2009
Posts: 14
Hi Marco,

thank you! The example was very helpfully.

Till
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

No problem ;-)

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.

Marco
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Aakash Goel wrote:
Another scenario:


implementation1:

Compiler says: A.a() not defined.


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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Ankit,

sorry for confusing anybody here, of course you're right. I should have been more careful with my examples. So to hopefully clarify this:

The compiler will NOT complain with my example because the given method "void doSomething()" has exactly the same signature and therefore you simply have to implement it once in class C!

The compiler WILL complain with if the methods in interface A and B have exactly the same signature but differ only in their return type!

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.

Better now? I hope so :-)

Marco
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Marco Ehrentreich wrote:Hi Ankit,

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
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...


Yes, I am sure. I just checked, again.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Well I tried it and I didn't get any error and neither should have you got any error...
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
Ankit Garg wrote:Well I tried it and I didn't get any error and neither should have you got any error...


my fault

I tried with


with


it works!

thanks for pointing out, Ankit.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: implementing two interfaces with the same method name!