I have no idea what your question is, so no I cannot help..
Would you like to explain the problem?
Wee Keong Soh
posted 9 years ago
thanks for the prompt response. Maybe i will try to rephrase my question.
I was reading the Headfirst book on interfaces and how it can solve the problems of multiple inheritance.
In languages where multiple inheritance is allowed, if a class extends 2 superclasses, the computer will be confused if both superclasses have the same method. As in it will not know which version to run.
The book explains that by implementing an interface (in which all the methods are abstract) can solve the problem. I don't really understand how the problem is solved by having a class implementing an interface.
Suppose you have classes A and B, which both have a method called doThis(). Next suppose you had a class W which extended both A and B. Let's also suppose you didn't override doThis() in your definition of W.
So here's the problem you are asking about: if you create an object of class W and call its doThis() method, does that result in calling the doThis() method of class A, or of class B? It appears you understand why this is a problem, and that you can't actually do that in Java.
Now suppose that B is an interface instead. It declares a method called doThis(), but doesn't provide any implementation for it. Any class that implements B must provide the implementation. Again suppose you have a class W which extends A, and now implements the interface B, and again let's suppose you don't override doThis() in its definition.
Now if you create an object of class W and call its doThis() method, what happens? You should be able to figure that out, it's like the previous example only this time there is no problem. And you can actually do this in Java. Write the code yourself and you'll see that it can compile and run.
Wee Keong Soh
posted 9 years ago
In other words, even though class W inherited the method doThis() from class A, by implementing the doThis() method from interface B, you are telling the JVM during runtime that if the doThis() method is invoked, refer to the one implemented from interface B.
It points the JVM in 1 clear direction rather than leaving it confused over which version to run.
Interfaces don't have implementation, so having several doThis() methods in several interfaces means the compiler has to choose from several "nothing"s. As long as there is one implemented method, it is happy.
If you inherit a non-abstract method from a superclass, that is only one implementation, so all is well.
You get problems with different interfaces if the methods have different return types; that cannot be disambiguated, so the interfaces would be incompatible with each other.
You should also not implement two interfaces where the methods with the same name have different intentions; you can find the intentions in their Javadoc documentation.
There is no risk, with these constraints, of different implementations from different sources interfering with each other.
Other languages have different techniques; in Eiffel for example, you have to specify which of the superclass methods you want to use.
Curse your sudden but inevitable betrayal! And this tiny ad too!
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database