aspose file tools*
The moose likes Beginning Java and the fly likes About Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Interfaces" Watch "About Interfaces" New topic
Author

About Interfaces

shankar reddy
Ranch Hand

Joined: Jun 04, 2007
Posts: 71
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.


Java Lover<br /> <br />Shankar Reddy <br />SCJP1.4 (88%)
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Interfaces