Java: How to implement the interface?

How do I implement a method in java (lets call it as A) which is present in interface B as well as abstract base class C?

  • 1) What  would happen if both the interface method definition and abstract Base class method definition are same? What to do if they are different?

  • Answer:

    The simplest case is when the method signatures are identical. That means that the return type, the name, the number of parameters, and the types of each parameter match. In that case, if the interface B, the abstract class C, and the concrete class A all have identical method signatures, then at both compile and run-time, Java will consider those to be identical, such that if A extends C and implements B, the method on A will be selected first, it will "super" to the method on C, and (starting with Java 8), there can be a default implementation on interface B. The next simplest case is when the method signatures are different enough as to not confuse either the compiler nor the runtime. For example, if the methods have a different number of parameters. In this case, there is no collision at either compile or runtime, because there is no ambiguity in terms of which method is intended to be called. The complex case is when the method signatures are ambiguously colliding, for example when they differ by return type, or only by the types of certain parameters. To understand this complex case, you must first focus on a particular version of Java (since the rules have been refined e.g. w.r.t. return types), and you should read the relevant sections of the Java Language Specification. For example, section 8.4.8 of the JLS version 3.0 is titled "inheritance, overriding and hiding", and covers part of this topic. Section 15.12 goes into great detail about how the compiler determines what method is being referred to by the code. (Having personally written a Java compiler from scratch using only the Java Language Specification as a guide, I can state without reservation that it is one of the best written technical pieces in the history of our industry. Kudos to the authors, including my office neighbor at work, Guy Steele. :-) For the sake of full disclosure, I work at Oracle. The opinions  and views expressed in this post are my own, and do not  necessarily reflect the opinions or views of my employer.

Cameron Purdy at Quora Visit the source

Was this solution helpful to you?

Other answers

There will be only two cases in ur question: 1. method implemented in abstract class 2. method not implemented in abstract class in first case we no need to implement the method from interface as it is already implemented(remember extends come before implements) second case you should implement the method, even though method is declared in both interface and abstract class implementation is only one.

Anonymous

Case 1 : method signature and return types are identical interface B and abstract class C Overriding the method in A ( implements B and extends C) will work fine Case 2 : method names are identical in B and C but signatures are different If A implements B and extends C, then A need to  provide implementation to the method in B and might also need to provide implementation to the method in C ( if the method is abstract). The two methods with different signatures will be treated as overloaded methods Case 3 : method names and signatures are identical in B and C but return types are different If the return types of the two methods are not comparable, then you will get a compilation error If the return types of the two methods are classes in hierarchy (say one of them returns List while other returns ArrayList), everything will work fine as long as the implemented method in A has return type as the sub type and not the super type (ie ArrayList and not List) Case 4 : method name, signature and return type are identical in B and C but the Exceptions in throws clause differ If the two methods throw different checked exceptions  (e.g say one of then throw IOException while other throws InterruptedException), then its perfectly legal if the inplemented method in A throws no Exception but it cannot throw just either of the two Exceptions or both the Exceptions

Kaustubh Saha

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.