Java: How to implement the interface?

How do you implement multiple inheritance through interface in java?

  • Answer:

    In this question there is a very good trick had been used by James gosling. Actually java doesn't supports multiple inheritance. Only interface can be used to do so. it is because java supports inheritance. Java also supports classes within classes. So in Interface that classes within classes mechanism is there. This mechanism is behind the keyword Interface . The keyword Interface means it is classes within classes. so By implicitly we are using multiple inheritance by the use of interface.. The multiple inheritance is supported by using more implements key word example class a implements b, implements c, implements d { .... } so here the class a supports multiple inheritance by implementing class b,c,d. Java does not support true multiple inheritance - true M.I. requires that a class be able to inherit directly from multiple classes. That is, in M.I., Class A can be a direct child of Class B and C. To get much of the functionality and advantages of the M.I. model, Java uses Interfaces. Thus, a Java Class A will be the child of Class B, but can implement several interfaces X, Y, and Z. This attempts to mimic the behavior that in a M.I. language would be given by Class A being a direct child of B, X, Y, and Z. The major difference between a M.I. language and a single-inheritance language with interfaces (like Java), is that interfaces can only define the method signatures and constants; they CANNOT provide implementations of those methods in the interface specification. Thus, any class which implements a given interface must provide an actual implementation of the methods that the interface describes. The big downside to this is that it can lead to a large amount of "code copying" - that is, if both Class A and Class B implement an interface X, there is a good chance that most of the methods from X will have the same code implementation in both Class A and B. Sometimes this is unavoidable; however, it is also considered good programming style to use abstract Classes to implement commonly used interfaces, then put that abstract class fairly high is the object heirarchy, allowing large numbers of classes to inherit from that abstract Class (which, in turn, means they have access to the implementation of those interfaces without having to copy the code). The advantage of interfaces is that they can keep things conceptually clean, and also make it simple for classes to decide how they want to implement the method contract the interface describes. Practically speaking, if you would like Class A to inherit from Class B and also to inherit from Class C, then, in Java, you would simulate this via interfaces this way: public class B { // the superclass methods_go_here_with_their_implementations; } public interface C { // the Java equivalent of Class C in a M.I. Language the_method_signatures_of_C_go_here; } public class A extends B implements C { implementations_of_methods_in_C_go_here; }

community wiki at wiki.answers.com Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.