From: Chauk-Mean P Date: 2007-04-04T03:08:16+09:00 Subject: Re: New presentation on Ruby ------=_Part_23141_14173180.1175623691431 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 2007/4/3, Ilan Berci : >> >> As stated in slide 15 : >> - I just said that Java untyped collections or generic collections for >> type >> Object DO support heterogeneous types. >> But the drawback is that you NEED to downcast a retrieved element. >> - Conversely, if you use a generic collection for a type X (e.g. >> TalkingAnimal) different than Object, you can put only objects of type >> X. >> You cannot put an object that is not of type X (e.g Rabbit2) >> You avoid most downcasts for retrieved elements. Thus you can retrieve a >> TalkingAnimal and call talk. >> But if you put an object of type Y that extends the capability of X >> (e.gDuck2) and you want to call a method of Y ( >> e.g. walk), then you need to downcast to Y. > >Chuck, please update your java from 1.4 to 1.5JDK (5.0) and check out >generics, there is no longer a NEED(why the upercase?) to downcast from >containers as it's done for you. Ilan, First, my first name is Chauk-Mean not Chuck :-) Regarding Java 1.5, I think that I'm up to date. Indeed, the second example from the slide 15 uses generics (please take a second look at it) : Map menagerie = new Map ... Yes, a talking animal can talk directly and there is no need to downcast TalkingAnimal duckObject = menagerie.get("my duck"); duckObject.talk But if you want your object (which happens to be a duck ) to walk, you have to downcast it to Duck2 Duck2 duck = (Duck2)duckObject; duck.walk; As I said, with generics you avoid most downcasts but there are cases like this one which require downcasts. > >I also fully realize that I didn't hear the talk that went with the > >slides but you asked us for feedback on the slides knowing that we > >didn't hear the presentation. Be carefull what you wish for especially > >on this list.. You're right. It seems that I have to make an annotated version with some additional comments. >As for your example on duck typing, I can illustrate the same thing in >your slides by doing an exact port in Java which is known not to support >duck typing (message passing) >class Duck > >{ public String quack() {return "quack";} } > > >class Bird > >{ public String quack() {return "chirp";}} > > >d = new Duck(); d.quack(); > >b = new Bird(); b.quack(); // does this mean java supports duck typing > too?? It does if we go by your slide Hey, you have to declare the type in Java. And of course, it will work if you declare the type like : Duck d = new Duck(); d.quack(); It's a detail that is important. It's the same for C++. Chauk-Mean. ------=_Part_23141_14173180.1175623691431--