This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Interfaces question, Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces question," Watch "Interfaces question," New topic
Author

Interfaces question,

Sunali Anu
Greenhorn

Joined: Jun 04, 2007
Posts: 14
Can a class implement two interfaces that each contains the same method signature? Can some body explain with examples please...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This can be easily tested.

[ June 23, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Francesco Bianchi
Ranch Hand

Joined: Jun 22, 2007
Posts: 62


Classes can implements multiple interfaces. This is possible because even if a equally-named method is declared in both of them, the JVM don't get mad choosing which implementation to use. There is only one... Interfaces is more about giving a class a different... "face" if you want

On the other side you cannot say the same for inheritance. If class A has a method implementation (let's say doSomething()) and class B another implementation of a method with the same name as class A (again, doSomething())... and you make class C extend both of them...and C inherits both implementations because the 2 were both public... Once you write something like: , what do you think the JVM would do? Which specific implementation would you use? This problem is better known as Deadly Diamond of Death (cause of the picture you get if you write it in an UML class diagram).

Hope it's clearer now
[ June 23, 2007: Message edited by: Bianchi Francesco ]

SCJP 5 & 6, SCWCD 5, SCBCD 5
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Java doesn't support multiple inheritance so the problem which you describe with inheritance doesn't occur in JAVA.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Francesco Bianchi
Ranch Hand

Joined: Jun 22, 2007
Posts: 62
Originally posted by Remko Strating:
Java doesn't support multiple inheritance so the problem which you describe with inheritance doesn't occur in JAVA.


Thank you...I forgot to explicitely underline it...but that is what I was tryng to explain I cited the problem just to make it clearer why Java allow multiple interface inheritance and implementation while forbids class one.
Sunali Anu
Greenhorn

Joined: Jun 04, 2007
Posts: 14
Thanks everyone for your inputs... That's clear now..
Nafis Khan
Greenhorn

Joined: Jun 24, 2007
Posts: 1
hello i'm new here in this message board. i need some help. can anyone direct me to where i can post some basic beginner's questions concerning Java programming? actually i needed to know if there is any site where i can become a member and upload any new Java Program that i make and take part in competitions. please let me know, it would be very helpful
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Nafis Khan:
hello i'm new here in this message board. i need some help. can anyone direct me to where i can post some basic beginner's questions concerning Java programming? ...

Welcome to JavaRanch!

Actually, this forum is ideal for basic beginner's questions. We don't have coding competitions, but for answers to beginning questions, this is the place.
Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
Originally posted by Remko Strating:
Java doesn't support multiple inheritance so the problem which you describe with inheritance doesn't occur in JAVA.



What about this ??

interface A {int show();}
interface B {Object show();}

class AB implements A,B{}, which show method's implementation will you give in class AB??


ॐ सर्वे जना: सुखिनो भवन्तु , तथास्तु |
'May the whole world be happy, so be it'

SCJP1.5, SCWCD1.5
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,
I modified the above example to fit the above request:


And I get the following compile error:


And if I switch the return type to void, I get error that it dosen't fulfill the request for the other interface.
So, looks like its not allowed.


With Best Regards,
Shyam Prasad Murarka
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Shyam Prasad Murarka:
...And if I switch the return type to void, I get error that it dosen't fulfill the request for the other interface...

I'm not sure what you mean by that. This works for me...

Remember, you can't have methods in the same class that differ only by return type. And if a method's return type is void, then you can't try to return something (even a null reference).
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,
What I had meant was that if in my implementation of the Sunali class I had changed the return type to void then the compiler would complain that I had not satisfied the Programmer interface.
Anyway methods are not differentiated by their return type, which I had overlooked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces question,
 
Similar Threads
Will every class inherit the object class?
Interface
Interface question from Khalid and Rasmussen
java.lang.Object is a class or Interface
Abstarct Interface