• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Putting interfaces into objects

 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a class called TestCol that implements Comparable, how exactly is this code working:

I thought you had to do a cast to put an interface into an object, and the new object had to implement the interface. Is this just a special case because of Object?
Jason
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Type stuff:
"o" is a reference variable of type Object (a class type). It can refer to an object that is an instance of the class Object, to an object that is an instance of any subclass of Object, or to null.
("c" is a reference variable of type Comparable (an interface type). It can refer to an object that is an instance of a class that implements Comparable(, or to null -- I think).)
The object that "c" refers to is an instance of TestCol (regardless of the type of the reference variable). TestCol is a subclass of Object (either directly or through some intermediate class). Therefore a reference variable of type Object can refer to instances of TestCol.
(Reference variables can have a class type or an interface type. Objects, as instances of classes, can have only a class type, not interface type.)
Assignment stuff:
An assignment of the type < variable> = < expression> must be type compatible.
In our case, the expression happens to be an interface type. The interface reference type can be implicitly (up)cast (maintaining type compatibility) to the variable's reference type if (1) the variable's reference type is Object (woo hoo!) or (2) the variable's reference type is a superinterface of the expression's subinterface type.
The above paragraph is a rough paraphrase of material on page 204 of Mughal and Rasmussen's A Programmer's Guide to Java Certification: A Comprehensive Primer.
Pretty clear, no?
So there is something vaguely special about implicitly casting an interface reference type to the class reference type Object.
For what it's worth, despite the kookiness of the language I've used, I'd recommend moving away from the idea of being able to "put interfaces into objects" and try to think in terms of reference variables and objects having types (that do all sorts of weird type compatibility dances).
(Your headache's getting contagious, Jason.)
[This message has been edited by Michael Matola (edited December 08, 2001).]
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every runtime thing is a java.lang.Object subclass, directly or indirectly. You can therefore assign an Object reference to any run-time non-primitive you want. This is a compiler "liberty" known as automatic promotion; it allows any parent type to refer to a child run-time object without explicitly telling the compiler it's ok.
Consider a class like Applet. Applet extends Panel extends Container extends Component extends Object. Automatic promotion lets us refer to an Applet by any (super)parent reference type. If we were counting, for example, all the Panel objects in our program, we could go through and say "if I find an Applet, that counts as a Panel, so increment one..." or you could just add an Applet to a Vector of Panels and then count the Vector without ever caring what subtypes of Panel are in it.
With an Object reference, however, the compiler can only verify Object's method calls against the run-time thing. If you're really pointing to an Applet, the compiler rejects calls to init() or paint() unless you downcast to the appropriate type:

An interface operates as a supertype in this sense. It both constrains and generalizes an object's run-time behavior. If we have a Runnable object, for example, we can instantiate a Thread object with it, call that object's start(), and the Runnable will do its run() thing regardless of the run-time object's internal makeup.
That's not only legal, it's very, very powerful. The Runnable interface, properly applied, reduces my "need-to-know" about any Runnable class down to a single method. You want that power, I guarantee it.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide

[This message has been edited by Michael Ernest (edited December 10, 2001).]
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Michael Matola:
In our case, the expression happens to be an interface type. The interface reference type can be implicitly (up)cast (maintaining type compatibility) to the variable's reference type if (1) the variable's reference type is Object (woo hoo!) or (2) the variable's reference type is a superinterface of the expression's subinterface type.


That's what I was looking for. I should preface all of this with I was playing around with this stuff because I remembered reading stuff about this in y'alls book Mr. Ernest , but then was reading another cert book (the same one which stated doing String s = "Hello" ; s = null ; meant that "Hello" coupld be GC'ed) and they provided an example that I found rather strange. They stated that as long as the object you are sticking the interface type into implemented that interface you could cast to that new object type. That appears to be incorrect.
I understand for the most part all the rest of the reference type casting, it was the whole interface type into an object that was strange. Believe me Michael, this is something I normally wouldn't run around doing, just wanted to make sure I understood it (for the test and for knowledge in general).
Thanks guys!
Jason
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, now that I have confused everyone including myself, I need to reask this.
You have an interface called Implementable (object imp), and you have two classes, OneClass and TwoClass (object oc and tc, respectively), that implements Implementable. I can do this:
imp = oc ; //no cast needed.
imp = tc ; //again, no cast needed
tc = imp ; //compiler error
tc = (tc)imp ; //compiler happy
Correct? The only time you don't have to make an explicit cast is when doing:
Object o = imp ;
Is that correct? My biggest hang up about this I guess is never thinking of interfaces in terms of Object. Interfaces aren't a subclass of Object, are they? I thought interfaces could only extend other interfaces (even allowing multiple extensions).
Jason

[This message has been edited by jason adam (edited December 08, 2001).]
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ja> Ok, now that I have confused everyone including myself, I need to reask this. You have an interface called Implementable (object imp),
You can't have an "object imp." You have a reference variable of type Implementable (an interface type) called "imp." "imp" can point to an object that is an instance of any class that implements Implementable (or a subinterface of Implementable).
ja> and you have two classes, OneClass and TwoClass (object oc and tc, respectively), that implements Implementable. I can do this:
ja> imp = oc ; //no cast needed.
ja> imp = tc ; //again, no cast needed
ja> tc = imp ; //compiler error
ja> tc = (tc)imp ; //compiler happy
I believe you mean the following for the last line:
tc = (TwoClass)imp ;
ja> Correct? The only time you don't have to make an explicit cast is when doing:
ja> Object o = imp ;
ja> Is that correct?
There's one more situation where you don't need an explicit cast when the < expression> is an interface type -- when the < variable> is a superinterface of subinterface type < expression>. See the code at (1).

ja> My biggest hang up about this I guess is never thinking of interfaces in terms of Object. Interfaces aren't a subclass of Object, are they?
"Interfaces aren't a subclass of Object, are they?" is a question that I can interpret in two ways. One way I can answer, the other way is a question that, given my current level of Java knowledge, I would not even attempt to answer.
(1) If you meant "Classes that implement an interface are subclasses of Object" then the answer is yes. Every class in Java is ultimately a subclass of Object, regardless of whether it happens also to implement an interface.
Remember, when you declare a class that doesn't explicitly extend anything, it implicitly extends Object. So the following are equivalent:
class MyClass
{
}
and
class MyClass extends Object
{
}
I've seen some code you've written, Jason, that has a class that implements Comparator. The Comparator interface calls for two methods to be in implementing classes: compare() and equals(). In your class definition, you provide an implementation for only compare(). Why does your code still compile?
(Remember this conversation, by the way?)
(2) If you meant that an interface definition can be thought of as extending Object, that the following are equivalent,
interface MyInterface
{
}
/* Not valid Java code */
interface MyInterface extends Object
{
}
then that's not true. The compiler screams:
C:\Michael\javaranch\projects\stuff\MyInterface.java:1: interface expected here
interface MyInterface extends Object
^
if you try to compile the second code snippet, because interfaces can only extend other interfaces.
(At this point it's somebody's job to point out that as valid Java code the first example does indeed produce a *.class file and to raise the question of what's going on inside the guts of Java -- could it be compiling MyInterface to something kind of Objecty? This is where I bow out of that conversation. I just don't know enough about Java to say.)
ja> I thought interfaces could only extend other interfaces (even allowing multiple extensions).
Righto. What's leading you to think otherwise?
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Michael Ernest:
Everything is a java.lang.Object subclass, directly or indirectly.


"An interface declaration introduces a new reference type..." JLS (second edition) chap 9

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

Maybe since you cannot instantiate an object of the interface type itself, the compiler knows that any interface "thing" that you are assigning must be an implemented object.

[This message has been edited by Marilyn deQueiroz (edited December 09, 2001).]
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Undestand everything you put in the example Michael, it's just the whole interface into Object that I'm fuzzy on.
I understand that imp is really holding a reference to an object that implements the interface and not an interface object because of course you can't create an object from an interface. Got that concept down (even though I sometimes garble the language up). That object being held in imp is, like all objects, a subclass of Object. For some reason I just kept thinking a cast should go there to go from an interface type to an Object. It was the tc = (TwoClass)imp; thing. However, with Object o = imp; we're going from a generic interface type to a more generic Object type, there's no cast!
Thank you for showing me the light y'all
Jason
[This message has been edited by jason adam (edited December 09, 2001).]
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Marilyn deQueiroz:
Maybe since you cannot instantiate an object of the interface type itself, the compiler knows that any interface "thing" that you are assigning must be an implemented object.


Now how is it that Marilyn can say in 2 lines what it takes me 200?
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Older and wiser... emphasis on the older
*ducks and runs to hide*
umm... Michael, yeah, that's who I am
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Marilyn deQueiroz:
Originally posted by Michael Ernest:
Everything is a java.lang.Object subclass, directly or indirectly.
"An interface declaration introduces a new reference type..." JLS (second edition) chap 9

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

Maybe since you cannot instantiate an object of the interface type itself, the compiler knows that any interface "thing" that you are assigning must be an implemented object.


I wasn't sure if Marilyn's post supports or rebuts my original statement. To clarify, however, I've amended my post to read "Every runtime thing."
[This message has been edited by Michael Ernest (edited December 10, 2001).]
 
I think she's lovely. It's this tiny ad that called her crazy:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic