• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

implementing two interfaces with the same method name!

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:
As pointed out earlier...



When?

I got the point though. thanks.
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!

I'm glad that it really helped you

Marco
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marco,

thank you! The example was very helpfully.

Till
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I tried it and I didn't get any error and neither should have you got any error...
 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic