aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why an interface type assignable to Object type? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why an interface type assignable to Object type?" Watch "Why an interface type assignable to Object type?" New topic
Author

Why an interface type assignable to Object type?

Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443

The JLS says that(JLS 9.1.2) :

While every class is an extension of class Object, there is no single interface of which all interfaces are extensions.

So if an interface is not derived from Object, and Object does not implement any interface, and Object and interface are different types, then why is it allowed to assign an interface type to an Object type, and NOT vice-versa?
Thanks.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
why is it allowed to assign an interface type to an Object type

1. i is a reference to an object that implements I. At run-time, the actual type of the object will be a subclass of Object.
2. By JLS 9.2, the interface I implicitly declares abstract versions of all of the methods of Object. The type I is a subtype of Object.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is an explanation that morphs from the specific JLS requirement to the general OO principle.
The type conversion from I to Object is a widening reference conversion (JLS 5.1.4).
I is a subtype of Object.
An instance of a class that implements type I is always a subclass of Object.
It is possible to substitute instances of type I for instances of type Object in any situation with no observable effect.
From I to Object satisfies the Liskov Substitutability Principle.
[ August 25, 2003: Message edited by: Marlene Miller ]
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Thanks Marlene
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Alton, if understood subtypes better I could give you a better answer. I need to learn more about programming languages in general. Some day you and I will have to study a book like this Concepts in Programming Languages, table of contents, slides
[ August 25, 2003: Message edited by: Marlene Miller ]
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127

Wiiickeeeed!
(need sleep. had to think hard and read slooo oo o. cool.)


<img src="cool.gif" border="0"> <img src="graemlins/beerchug.gif" border="0" alt="[beerchug]" /> <br />SCJP 1.4
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Alton and Marlene
Isn't it because of this bug.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Anupam Sinha:
Isn't it because of this bug.

No, the two issues are unrelated. That bug has more to do with particular compiler error messages that you get when trying to run Object methods against an interface reference type.
Things like this must obviously work in a real program because the interface reference type must be referring to an actual class object at runtime:
Interface i = new RealClass()
String s = i.toString();
But where is the toString() method defined? If interfaces don't extend Object then how can they have a toString() method?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Anupam, thank you for showing me the Bug Report 4644627.
I don�t think Object o = i is related to that bug. The bug is about reflection, method invocation and protected access. However, if you would like, you can explain why you think Alton�s question is related to the bug.
I think Object o = i is a direct application of JLS 5.2 assignment conversion context and JLS 5.1.4 widening conversion, from any interface type to Object.
However, I struggled to explain why interface to Object is a widening conversion. One can appeal to intuition - the type of an object that implements an interface is always a subclass of Object.
I thought about using JLS 9.2, but no.
I finally decided interface type I is a �subtype� of Object. So what does �subtype� mean? It means the principle of substitution is satisfied. An instance of one type can be substituted for an instance of another type in any situation with no observable effect.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
But where is the toString() method defined? If interfaces don't extend Object then how can they have a toString() method?

Thomas, isn't that JLS 9.2. An interface with no direct superinterfaces implicitly declares the methods of Object.
interface I {
public String toString(); // supplied by the compiler
}
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Marlene Miller:

Thomas, isn't that JLS 9.2. An interface with no direct superinterfaces implicitly declares the methods of Object.
interface I {
public String toString(); // supplied by the compiler
}

Exactly. That bug report is directly related to that issue.
I believe we have had this conversation before.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Yes, we have.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Hi all,
Thanks everyone for your inputs and thanks Anumpam for that link.
From that bug report:

But the JLS 9.2 says that the interface "implicitly defines" those public
methods from Object, and doesn't extend Object.

This is really why I asked the question in the first place. I can't quite reconcile the fact that the interface merely defines methods of Object. This should not make interface a subtype of Object. Unless of course interface implicitly extends Object. Then that would explains everything.
Originally posted by Marlene Miller:

I finally decided interface type I is a �subtype� of Object. So what does �subtype� mean? It means the principle of substitution is satisfied. An instance of one type can be substituted for an instance of another type in any situation with no observable effect.

This is also another good way of explaning it.
[ August 30, 2003: Message edited by: Alton Hernandez ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I get nervous when I use the word "subtype". "subtype" is a special concept in formal treatments of programming languages.
I have seen this book for a course at my local university. Types and Programming Languages
However, the baby explanation is this. A type specifies a set of values and the operations that can be carried out with those values. A subtype contains a subset of the values of a given type. You can substitute a value of a subtype whenever a supertype value is expected.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
An interface type and the spawned class file:

No Object methods are defined, but the entry "super 2" points to constant pool entry 2, that declares the super type of Inter. It is java.lang.Object!


SCJP2. Please Indent your code using UBB Code
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561

11775u
11472r
9963c
10165e
7046F
10569i
1086cl
10165e

1086cl
Of course it's java.lang.Object!


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi all
Well I realise that I was wrong.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Jose, thank you very much for the class file picture. How would you answer Alton's original question?
Andres, I don't understand your comment about 108 6c |. What does that mean?
Anupam, look at all the things we learned as a result of your post.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Marlene,
I think JLS 9.2 does not state whether an interface extends Object. The compiler, however, is clear about that!
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Jose.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Marlene Miller:
Andres, I don't understand your comment about 108 6c |.

I don't either.. I've been following this topic since the beginning, but I just got lost with that post.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Jose Botella:
Marlene,
I think JLS 9.2 does not state whether an interface extends Object. The compiler, however, is clear about that!

Actually the JLS specifically states that interfaces do not extend Object. But the JLS does say this:

If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method M. with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.

And this:

While every class is an extension of class Object, there is no single
interface of which all interfaces are extensions.

As noted above, however, that is not how the compilers actually work, however.
See Bug Id 4479715:

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.

The compilers do not agree with the JLS but no one really cares so it is doubtful that this will change.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why an interface type assignable to Object type?