Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cast to Comparable<T> ??? - Can someone explain?

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ladies/gents,

It's been a while and I'm looking at returning to grad school next semester, so I've dug out the DS&A book. One particular thing I don't understand:

This is a generic method to be used with an ordered array-based list to search for a good insertion point for an object. I totally follow the logic of the algorithm, but the line:

Comparable<T> temp = (Comparable<T>) list[loc]

really gets me. Comparable is an interface, is it not? If so, how can you then successfully compare this object that was cast to Comparable<T> if the method compareTo() is not implemented (interfaces are classes with no implemented methods, correct?)? I don't get my book's explanation:

'note that list[loc] is a reference of the Object type which was type cast to T. Now the class Object does not contain the method compareTo(). So we first create a reference of the type Comparable<T> and then use the compareTo() method. Also note that this method would work on the objects of only those classes that implement the interface Comparable and provide an appropriate definition of the method compareTo()'

OK, so, if this class doesn't implement the compareTo() method, and the Comparable<T> interface doesn't, then, following the above explanation ('this method would work on the objects of only those classes that implment the interface Comparable AND provide and appropriate definition of the method compareTo()' ), this shouldn't work, right? Or am I not looking at it correctly?

I remember really disliking this class when I took it, and I think it was becuase I just tried to trudge through it myself and didn't ask enough questions. I'm determined to understand the material inside-out this time, so any help with an explanation would be greatly appreciated.

Method:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Patrick,

Imagine you have a paper bag in your hand. You're in a darkened room. You can feel that the bag is heavy; there's something in it. Furthermore, you can feel that it's full of lots of little things, all the same size. But you don't know what.

Now I tell you, "the bag is full of peanut M&Ms." You say "cool!," grab a handful, and toss them into your mouth. Next, one of two things happens:

1) You bite into a delicious handful of peanut M&Ms.
2) You break all your teeth because the bag is actually full of marbles. Ouch.

OK, why am I telling you this?

a) The bag is an "array of Object".
b) My telling you "the bag contains peanut M&Ms" is casting the bag's contents
c) The broken teeth is what happens if the cast is wrong!!

Actually in Java, you don't even get that far. Casts are checked, so the line in question will throw an exception if the cast is incorrect. The code will work fine if, and only if, the cast is correct -- i.e., if the array really does contain objects that implement Comparable<T>. Otherwise it will fail. Simple as that.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:Hi Patrick,

Imagine you have a paper bag in your hand. You're in a darkened room. You can feel that the bag is heavy; there's something in it. Furthermore, you can feel that it's full of lots of little things, all the same size. But you don't know what.

Now I tell you, "the bag is full of peanut M&Ms." You say "cool!," grab a handful, and toss them into your mouth. Next, one of two things happens:

1) You bite into a delicious handful of peanut M&Ms.
2) You break all your teeth because the bag is actually full of marbles. Ouch.

OK, why am I telling you this?

a) The bag is an "array of Object".
b) My telling you "the bag contains peanut M&Ms" is casting the bag's contents
c) The broken teeth is what happens if the cast is wrong!!

Actually in Java, you don't even get that far. Casts are checked, so the line in question will throw an exception if the cast is incorrect. The code will work fine if, and only if, the cast is correct -- i.e., if the array really does contain objects that implement Comparable<T>. Otherwise it will fail. Simple as that.


Thanks for the response! Let me be more specific: what I don't understand is the line:

Comparable<T> temp = (Comparable<T>) list[loc]

It looks as if we are casting the object located at list[loc] to an object of type 'Comparable'. What is this? Is 'Comparable' a type of object, as well as an interface? Objects can't be instantiated from an interface, correct? If not, how can we have a 'Comparable' object? I guess this is what I don't understand. Thanks again!
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can cast an object reference to an interface reference as long as the object implements the referenced interface.

I think that's what's tripping you up. Or is it the <T>?
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To go off of Ernest's example, instead of telling you the bag was full of peanut M&M's were just telling you it's full of "Edibles" or "things that can be eaten". The same scenario plays out as before but an "Edible" is not a solid object, it's a concept or a description of an object. The peanut M&M is still edible so it can be safely cast to one even though edible is an interface.

HTH
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Legg wrote:You can cast an object reference to an interface reference as long as the object implements the referenced interface.

I think that's what's tripping you up. Or is it the <T>?


Ah-hah, thank you, sir. Yes, that's exactly what I wanted to know. So, really, even thought the code compiles, you could get in trouble by not watching what you pass into the method. The onus, then, is on the programmer to make sure that anything passed into the method implements compareTo() in this case, right?
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and no, you should know if what your passing implements Comparable or not but if you make a mistake the compiler will stop you with a warning before you get further. That's the great thing about defining what type of objects a collection holds. I couldn't tell you for sure with you code though as it's incomplete. Your code doesn't show how or where length and list are defined.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic