• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why an interface type assignable to Object type?

 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marlene
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wiiickeeeed!
(need sleep. had to think hard and read slooo oo o. cool.)
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alton and Marlene
Isn't it because of this bug.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, we have.
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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


1086cl
Of course it's java.lang.Object!
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
Well I realise that I was wrong.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jose.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic