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?
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.
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.
A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit.
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.
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With