• 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

Interfaces question,

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This can be easily tested.

[ June 23, 2007: Message edited by: marc weber ]
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for your inputs... That's clear now..
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic