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

Does Interface avoid Multiple Inheritance problems?

frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
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

Joined: Aug 20, 2001
Posts: 1817

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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
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

Joined: Dec 10, 2001
Posts: 7023
I wouldn't say that Java solved the naming conflict as much as Java solved the dual implementation with inheritance conflict.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Does Interface avoid Multiple Inheritance problems?