Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two methods when implementing interface containing only one

I created interface TwoMethods. Source code:

interface TwoMethods<T>
{
    public void method(T t);
}

Then I created class implementing this interface and after disassembling I saw 2 methods. Class:

class A implements TwoMethods<A>
{
    @Override
    public void method(A a) {}
}

After disassembling:

class A implements TwoMethods<A> {
   A();
   public void method(A); //first
   public void method(java.lang.Object); //second
}

Similarly for Comparable interface. Why when I create parametrized interface I have 2 methods. It is always, when I use parameter? I have additionally method with Object as argument?

like image 897
Pawel Avatar asked Dec 25 '13 10:12

Pawel


People also ask

Can I implement only one method of interface?

Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class. Implement every method defined by the interface.

Can an interface have 2 methods?

No, its an error If two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously.

What is another name for an interface that contains only one method?

A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit.

What to do if a class implements two interfaces which coincidentally have one method with the same name and signature?

Question 2: What to do if a class implements two interfaces which coincidently have one method with the same name and signature? Implement both interfaces implicitly. Implement at least one interface explicitly. A class cannot implement multiple interfaces.


2 Answers

method(java.lang.Object) is called the bridge method and it's generated because of type-erasure at compile time.

See Effects of Type Erasure and Bridge Methods

like image 158
Eng.Fouad Avatar answered Sep 28 '22 06:09

Eng.Fouad


If we look at interface TwoMethods bytecode we will see that the actual method is

public abstract method(Ljava/lang/Object;)V

that is at bytecode level information about type parameter does not exist, type is erased, JVM simply does not know about generics, type parameters are replaced either with Object or in case if T extends X with X. So from the point of view of JVM

class A implements TwoMethods<A> {
    public void method(A a) {
        ...

method(A a) does not override interface method because in bytecode it is in method(Object obj) can override it. To fix this problem compiler builds an implicit method, so called bridge method, in class A

public void method(Object obj) {
     method((A)obj);
}

visible in bytecode only. Now for this code

A a = new A();
TwoMethods<A> tm = a;
tm.method(a);

compiler will replace tm.method(a) with call to the bridge

   INVOKEINTERFACE test/TwoMethods.method(Ljava/lang/Object;)V

and this will redirect the call to A.method(A a);

like image 43
Evgeniy Dorofeev Avatar answered Sep 28 '22 05:09

Evgeniy Dorofeev