Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
SCJP2. Please Indent your code using UBB Code
The root of the problem is that javac represents an interface using the same representation that it will put it into the class file; that is, an
interface extends Object. That doesn't agree with the specification in 9.2, which says that the public members of Object are inserted directly into the interface but the interface doesn't extend Object.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
interface I {}
public class A implements I {}
public class B {
public static void main(String[] args) {
I i = new A();
i.toString();
}
}
Originally posted by John Zoetebier:
Forget about my previous posting.
I now realize it misses the point.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I don't find toString().if no, then why does reflection on an interface find toString()?
Originally posted by Marlene Miller:
I don't find toString().
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
As I understand the JLS, an interface with no direct superinterfaces implicitly declares abstract methods corresponding to those methods in Object. It does not inherit the methods from Object. JLS 9.2The JLS specifies that interfaces must inherit the public interfaces of all methods in the Object class.
Originally posted by Marlene Miller:
As I understand the JLS, an interface with no direct superinterfaces implicitly declares abstract methods corresponding to those methods in Object. It does not inherit the methods from Object. JLS 9.2
[ April 15, 2003: Message edited by: Marlene Miller ]
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Originally posted by Marlene Miller:
Thomas, do you see how the following example captures the same issues as your example? (Or do I still misunderstand the question?
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I used the wrong example. If an interface doesn't inherit the methods of the Object class then I shouldn't be able to compile this:
--------------------------------------------------------------------------------
If the toString() method isn't part of the interface then the above should give a compile error.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
A compiler bug. javac implements the rules using inheritance instead of by explicitly inserting the members as per JLS2 9.2 bullet 3. This subtle and generally innocuous bug is the only known symptom (well, see also 4479715) of this compiler shortcut. Implementing the JLS2 directly would require making special cases in many places in the compiler for very little benefit. We are more likely to fix this by filtering out protected members that were inherited into an interface. [ TP-this is what they did ]
The JLS2 is correct as it stands, as it is hard to imagine a more concise specification that has the desired effect.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Cloneable.class.getSuperclass();
null
An interface does not extends from Object
Object.class.getMethods().length;
9
There are nine public methods declared in Object class
Cloneable.class.getMethods().length);
0
However an interface inherits none of them
Cloneable.class.getMethod("toString", null);
NoSuchMethodException
Because an interface does not inherit from Object
if interfaces don't inherit the methods of the Object class then I shouldn't be able to do this:
Since I does not inherit the methods of Object, the compile should fail.
If the toString() method isn't part of the interface then the above should give a compile error.
So as to the question, does an interface inherit from Object? The JLS says no but in fact they do.
An interface with no direct superinterfaces implicitly declares abstract methods corresponding to those methods in Object. It does not inherit the methods from Object. JLS 9.2
Francis Siu
SCJP, MCDBA