Sunday 27 March 2011

Multiple Inheritances in java and the controversy


In my early bachelor's degree when I was starting to begin familiar with languages like C and C++, Our teachers use to point us to this unique problem of multiple inheritance and how it causes ambiguity in the inherited class, the solution was to use virtual functions/inheritance, it seem to be a fix that was added to C++ as it was not thought earlier while designing the C++ language.

Later during my master's degree, this new language called "java" starting to pick up waves and I was kind of impressed with it earlier being a new language and always wanted to explore it. I remembered how C++ solved the problem of multiple inheritances, and was curious of how java would play with it.

Most of them believe that multiple inheritances are possible in java through java interface. However I have a difference in opinion here.

According to me inheritance is

  • Inheriting the implementation
  • Inheriting the declarations

Inheritance of implementation is inheriting the actual code that does the work when the method is called which is done in case of C++ multiple inheritance and which is not possible to do in case of java

Inheriting only the method declaration and variables for one or many such classes for which the implementation will be written in its subclass is called inheritance of interfaces which is a bit different from the concept of multiple inheritances and it’s only possible in java. An interface defines all of those methods that an object exposes to either its subclasses or other classes.

When you inherit from a class in Java you get both inheritance of interface and implementation. All methods in the super class become part of the subclass's interface. However, when you implement an interface you only inherit interface and must provide implementations for each method.

When a Java class implements multiple interfaces you do run the risk of naming clashes. A class can implement two interfaces that share the same method name and return type. Say for example you have the following interfaces:

public interface MyFirstInterface{
                public void myFirstMethod();
}
public interface MySecoundInterface{
                public void myFirstMethod();
}

A class called MyFirstInheritance can come and implement these two interfaces at the same time without difficulty. However there may be a problem. Chances are if this happens the resulting class with not be able to provide an implementation that satisfies the expected behavior of both the interfaces at the same time. Even if you go ahead and provide and implementation for myFirstMethod() this may not make sense if you simply view the object as an MyFirstInterface or MySecoundInterface.

So what's the solution? If you have access to the source code the best course of action is to rename the methods in either of the above cases. If you don't have access, you are stuck.

7 comments:

  1. Thanks for bring this one Vinod, in my opinion implementing multiple interfaces might give an impression that we are extending same/different method(s) from more interfaces. But in theory, you never have definition of a method in an interface and leaving subclass the freedom to choose which way they want to go. This surely doesn’t cause a diamond problem as you have nothing that will conflict when same method signature is being overridden from multiple interfaces.

    ReplyDelete
  2. Why can't I declare static methods in Java interface ?

    ReplyDelete
  3. You can have a static method in an interface and you can also define a function in interface, however the reason why its not correct is because static methods Java get resolves through its static references. Java will not bother looking for an instance of a class when attempting to execute a static method. This is because static methods are not instance dependent and hence can be executed straight from the class file. Given that all methods in an interface are abstract, the Virtual Machine would have to look for a particular implementation of the interface in order to find the code behind the static method so that it could be executed. This then contradicts how static method resolution works and would introduce an inconsistency into the language.

    ReplyDelete
  4. Very well written!! When I started reading, I was expecting some work around or something different than usual....to this well-known issue...!! But yes....the last six lines will definitely ask a genuine cross question to those who says that we can achieve multiple inheritance with interfaces in java.

    ReplyDelete
  5. i tried declaring static methods in an interface but it gives me compilation error "modifier static not allowed here".

    ReplyDelete
  6. The way interfaces work and the fact the matter the question of using static methods in interfaces arrise in the first place is that the concept for defining and declaring a static methods in an interface is valid, you can even support it in theory; however it is not allowed/correct practically in java as per the reason mentioned in my comment earlier.

    ReplyDelete
  7. May be to answer Gaurav's concern in different words – Static means single reference per Class level instance meaning it can be repeated in a program context. Inheritance means declaring something (method) that can be overridden and defined in its subclasses. If we could have override static method in Inheritance then that would violate meaning of static. So Java doesn’t allow static declaration of method in an interface. Same is true with final, private and protected methods.
    To go little dipper in you can have inner class in an Interface and that can hold static method which is away from interface implementation. Something like
    class Inner{
    private static String wow;
    public static String getInnerElem(){
    wow = "wow";
    return wow;
    }
    }

    ReplyDelete