English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

See the following program...
interface A {
public void add();
}
interface B {
public void add();
}
class Test implements A,B
{
public static void main(String args[])
{
Test t=new Test();
t.add();
}
public void add()
{
System.out.println("Hello"+"World");
}

}


Explain me which interface method will be implemented and how to implement the other interface's method.

2007-03-06 23:15:37 · 3 answers · asked by Siva 3 in Computers & Internet Programming & Design

3 answers

I tested this code and it worked. I took this question to the extreme level and journeyed through the JLS and JVM specs. I got about as technical, and detailed, an answer as I think is possible.

First of all, this is a bad question as there is no such thing as “implementation” in Java. That is a colloquial computer term, but nothing specific in Java. Therefore, I interpret this question two different ways; what interface is used at compile time, and what interface is used at run time?

Compile time:
At compile time the subsignatures are used to see if a member is overriding or overloading an inherited member. So add()’s subsignature in class Test is equal to that of the subsignatures in interface A and B.

“8.4.8.1 Overriding (by Instance Methods)
An instance method m1 declared in a class C overrides another instance method, m2, declared in class A iff all of the following are true:
1.C is a subclass of A.
2.The signature of m1 is a subsignature (§8.4.2) of the signature of m2.
3.Either
om2 is public, protected or declared with default access in the same package as C, or
om1 overrides a method m3, m3 distinct from m1, m3 distinct from m2, such that m3 overrides m2.
Moreover, if m1 is not abstract, then m1 is said to implement any and all declarations of abstract methods that it overrides.”

Neither interface is “implemented”, but as far as the compiler is concerned Test.add() is an overridden method for both A.add() and B.add(). Those relationships aren’t stored, and Test.add() isn’t tied in anyways to A.add() or B.add(), the compiler just made sure that A.add() was being represented, and then made sure that B.add() was being represented. The compiler doesn’t know or care that A.add() and B.add() also have the same subsignature (It would care if A or B was a superinterface of the other Interface.).

Run time:
I never understood why interfaces needed to be included with code after compile time. Today I found the answer:
“2.17.3 Linking: Verification, Preparation, and Resolution
…Implementations of the Java virtual machine may precompute additional data structures at preparation time in order to make later operations on a class or interface more efficient. One particularly useful data structure is a "method table" or other data structure that allows any method to be invoked on instances of a class without requiring a search of superclasses at invocation time.

The binary representation of a class or interface references other classes and interfaces and their fields, methods, and constructors symbolically, using the fully qualified names (§2.7.5) of the other classes and interfaces. For fields and methods these symbolic references include the name of the class or interface type that declares the field or method, as well as the name of the field or method itself, together with appropriate type information.”
So it appears a similar thing happens here. A.add() and B.add() are both resolved to Test.add(). Once again, both interfaces are used.

So by any interpretation of this question the answer is both interfaces are implemented because at both compile and execution the compiler or JVM is relying on the Interface as the template and verifying/resolving it against the class. And that information does not factor into the software when it performs that same task again for the next item it is attempting to reolvse/verify.

2007-03-07 07:54:52 · answer #1 · answered by Anonymous · 0 0

Both! An interface just specifies the "plan", not an implementation. Both A and B specify that the plan needs to have an add() method. Your Test class implements the plan, providing the implementation details that fulfill the contract with both A and B.

Objects instantiated from your Test class thus can be used as A type objects and as B type objects, because the definition of each just says they have to have an add() method.

2007-03-07 10:42:10 · answer #2 · answered by vincentgl 5 · 0 0

Neither - it's a semantic error (meaning it will error at compile time).
The way to resolve this conflict is to implement each interface in a seperate inner class within class Test.

2007-03-07 07:23:14 · answer #3 · answered by oracle128au 7 · 0 1

fedest.com, questions and answers