Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces question,

 
Sunali Anu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a class implement two interfaces that each contains the same method signature? Can some body explain with examples please...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This can be easily tested.

[ June 23, 2007: Message edited by: marc weber ]
 
Francesco Bianchi
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java doesn't support multiple inheritance so the problem which you describe with inheritance doesn't occur in JAVA.
 
Francesco Bianchi
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your inputs... That's clear now..
 
Nafis Khan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic