Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does Interface avoid Multiple Inheritance problems?

 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I often see the statement that the problem with multiple inheritence (in other languages) is that you can end up with two methods with same signature in the same class. Then it is stated that interfaces solve this problem. But why couldn't the class trying to implement the interface encounter the same problem? Is it just because that the compiler will catch this error and prevent it? Still sounds like we have a problem of some sort if you need to implement a method of same signature as one you have inherited.
[ August 27, 2003: Message edited by: herb slocomb ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem that's particularly acute in C++, and took many years to sort out, was what happens when two of your base classes both implement method foo(int). Which implementation do you inherit? I.e., if base classes implement two different versions, and someone calls mySubClass.foo(3), which bit of code should be executed?
It's even more complex with virtual inheritance (in which base classes you indirectly extend more than once are "smooshed" to remove duplicates.) The rules are complicated and for many years some compilers (*cough* Visual C++ *cough*) got them wrong.
In Java, you only have single inheritance of implementation, so this problem simply can't arise. You can never inherit more than one implementation of a method, by definition.
Now, it's true that you can implement more than one interface, each of which states that you must implement a foo(int) method. This isn't a problem at all. You implement one method, and it satisfies both interfaces. If this makes sense, then great; otherwise, then you've learned an important Java design lesson: never use short, simple names for interface methods, because they might collide.
There's one further problem: two interfaces might specify conflicting methods. For example, "void foo(int)" and "int foo(int)". If this happens, then you're simply prohibited from implementing both interfaces at compile time.
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
... then you've learned an important Java design lesson: never use short, simple names for interface methods, because they might collide.
There's one further problem: two interfaces might specify conflicting methods. ..


But it seems bogus to me to say that Java interfaces have solved that naming conflict problem with inherited methods. Its simply a compiler rule preventing the naming conflict right? No need for interfaces at all in that case if the compiler will prevent it, right?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has not solved the problem at all. You can still implement two interfaces, each of which declare an "void accept(Object obj)" or some such.
C# has solved the problem be allowing you to qualify the method, so that you can have these collisions (of course, once you resolve them ,they're no longer collisions...) Once Tiger is out and stabilized, Java should think about taking that feature from C#, too.
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joel McNary:
Java has not solved the problem at all. You can still implement two interfaces, each of which declare an "void accept(Object obj)" or some such.
C# has solved the problem be allowing you to qualify the method, so that you can have these collisions (of course, once you resolve them ,they're no longer collisions...) Once Tiger is out and stabilized, Java should think about taking that feature from C#, too.

Well back to my original question, why have I seen many different authors claim that Java interfaces solve the multiple inheritance problem of method naming conflicts??
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't say that Java solved the naming conflict as much as Java solved the dual implementation with inheritance conflict.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic