Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Interfaces

 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody clarify my doubt reagarding interfaces .

given program..

interface Left
{
int m1();
}
interface Right
{
void m1();
}
class MyClass implements Left,Right
{
public int m1()
{
}
}

if i am declaring like this ,, but i am getting compiletime errors.

what my intention is, i should implements the methods in those interfaces in my own (concrete)class. is there any alternative for that one.

i am very thankful if you resolve my problem.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Well it really is worth you having a look in the FAQ to see if there is any information about Interfaces there, and to also check the Sun java site for information (http://java.sun.com)

<edit>
Having read your post again, I think the reason you are getting the error is because the method is named m1 in both interfaces, and the compilor dosnt know which interface's method (m1) you are trying to implement.

You cant have two methods named the same in one class unless you are overloading the method, ie changing the parameter list.


</edit>

Hope that helps
G

[ July 04, 2007: Message edited by: Gavin Tranter ]

[ July 04, 2007: Message edited by: Gavin Tranter ]
[ July 04, 2007: Message edited by: Gavin Tranter ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could easily implement two interfaces that declare identical signatures. When somebody uses your class, the interfaces promise the method will be there, and sure enough it is. This will surely confuse people if the two interfaces document different meanings for the signature. Say InterfaceA says "left() returns true if you should turn left now" and InterfaceB says "left() returns true if this object was left behind" and InterfaceC says "left() returns true if this object is a liberal".

But, your problem came about because the two interfaces have similar signatures that are not quite identical. If you implemented both, you'd have a duplicate method name because return type is not part of the unique method identifier. If you didn't implement both, you'd fail the contract on one interface.
[ July 04, 2007: Message edited by: Stan James ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic