Compiling the following interface:
package test;
public interface MyInterface {
public void foo();
}
and checking the compiled code using javap -v -s test.MyInterface
shows the following (-s
prints member signatures):
Compiled from "MyInterface.java"
public interface test.MyInterface
SourceFile: "MyInterface.java"
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_INTERFACE, ACC_ABSTRACT
Constant pool:
#1 = Class #7 // test/MyInterface
#2 = Class #8 // java/lang/Object
#3 = Utf8 foo
#4 = Utf8 ()V
#5 = Utf8 SourceFile
#6 = Utf8 MyInterface.java
#7 = Utf8 test/MyInterface
#8 = Utf8 java/lang/Object
{
public abstract void foo();
Signature: ()V
flags: ACC_PUBLIC, ACC_ABSTRACT
}
My question is: Why is there java.lang.Object
in the constant pool, knowing that an interface does not inherit from the Object
class?
Also if I change the interface definition to:
public interface MyInterface extends Comparable<MyInterface> {
public void foo();
}
and run javap
, I get the following:
Compiled from "MyInterface.java"
public interface test.MyInterface extends java.lang.Comparable<test.MyInterface>
Signature: #7 // Ljava/lang/Object;Ljava/lang/Comparable<Ltest/MyInterface;>;
SourceFile: "MyInterface.java"
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_INTERFACE, ACC_ABSTRACT
...
What exactly is the purpose of including java.lang.Object
in the signature Ljava/lang/Object;Ljava/lang/Comparable<Ltest/MyInterface;>;
of the interface?
Also, if I try to view the bytecode using a tool (specifically JBE), it incorrectly shows that MyInterface
has java.lang.Object
as superclass with the class name java.lang.Object
saved in the constant pool:
Note: Using jdk1.7.0_75
java.lang.Object
is explicitly in the bytecode, then I was wondering why these implicit methods are not there as well. I think it's close to this reason but more about what the below answers and comments describe. – Grays