Reputation: 1
Below interface I
implicitly has the public methods of class Object
as abstract members. One of them is toString()
interface I{
void test(int i);
//implicitly has abstract members matching every public method of 'class Object'
// one of them is toString()
}
Below class W
inherits class Object
public methods, one of them is toString
,
class W implements I{
public static void main(String[] args){
I w = new W();
w.toString(); //How toString() method is resolved here?
}
}
As both, super interface(I
) and super class(Object
) of class W
have toString()
method,
How does javac
resolve w.toString()
at compile time? I learnt that invokevirtual
instruction is used here.
How does jvm resolve w.toString()
at runtime?
Upvotes: 1
Views: 122
Reputation: 280174
Because the method is invoked on an expression of type I
, an interface type, the bytecode will contain an invokeinterface
instruction for the corresponding method.
At runtime, the method invoked will be determined as such
Let
C
be the class ofobjectref
. The actual method to be invoked is selected by the following lookup procedure:
- If
C
contains a declaration for an instance method with the same name and descriptor as the resolved method, then it is the method to be invoked.- Otherwise, if
C
has a superclass, a search for a declaration of an instance method with the same name and descriptor as the resolved method is performed, starting with the direct superclass ofC
and continuing with the direct superclass of that class, and so forth, until a match is found or no further superclasses exist. If a match is found, then it is the method to be invoked.- Otherwise, if there is exactly one maximally-specific method (§5.4.3.3) in the superinterfaces of
C
that matches the resolved method's name and descriptor and is not abstract, then it is the method to be invoked.
In your case, objectref
is a reference to an object of type W
. W
does not contain a declaration for an instance method with the same name and descriptor as the resolved method. We therefore check the superclass of W
, Object
. Object
does have such a method. That method is therefore invoked.
Upvotes: 3
Reputation: 1459
Interface doesn't have any super class not even the Object class e.g.
public class Main {
public static void main(String[] argv) throws Exception {
Class cls = java.lang.Cloneable.class;
Class sup = cls.getSuperclass(); // will return null
}
}
Hence interface never override a method of an Object class.
When an interface has no direct SuperInterface, it will create abstract public method for all those public methods present in the Object class.
Upvotes: -2
Reputation: 2453
Interfaces don't override methods from any class. When you use the toString()
method, the default toString()
is executed from the Object
class because your class W
extends from Object
. For more clarification, override the toString()
method in your class W
.
@Override
public String toString() {
return "Yo, baby! M here!";
}
Then the output will be:
Yo, baby! M here!
Upvotes: 0
Reputation: 26946
An interface
doesn't override a method.
Also your class doesn't override the method toString()
. You have to rewrite the method toString()
to overerride it.
So simply it is called the implementation of toString()
of the superclass Object
.
Upvotes: 1