tag:blogger.com,1999:blog-741750605858169835.post7963118749054513663..comments2024-01-24T14:53:02.919+00:00Comments on Stephen Colebourne's blog: Generics - why no ?Stephen Colebournehttp://www.blogger.com/profile/01454237967846880639noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-741750605858169835.post-2698138554994206722006-08-02T01:51:20.000+01:002006-08-02T01:51:20.000+01:00If you have to implement each method in the subcla...If you have to implement each method in the subclass, what's wrong with the covariant return type as suggested by Keith in the first post as you would have to write the method signature out in any case?<br /><br />My initial thought was that you wanted a concrete implementation in an abstract superclass so you didn't want to write out the methods again in each subclass.Eugenenoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-81519768675609619072006-08-01T20:23:43.000+01:002006-08-01T20:23:43.000+01:00BTW, in case it wasn't clear, I was using as ...BTW, in case it wasn't clear, I was using as a non-existant syntax for something I want to do:<br /><br />public abstract class A {<br /> public withYear(int year) {<br /> return withMillis(calculateMillisForYear(year));<br /> }<br /> protected abstract withMillis(long millis);<br />}<br /><br />public class B extends A {<br /> protected B withMillis(long millis) {<br /> return new B(millis);<br /> }<br />}<br /><br />This kind of approach is very useful for an abstract superclass of multiple immutable subclasses.Stephen Colebournenoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-45789026325595832006-08-01T17:27:40.000+01:002006-08-01T17:27:40.000+01:00I would think it is because there are no objects a...I would think it is because there are no objects and no reference to "this" at compile time, which is the only place where generics existDan Hinojosahttp://www.evolutionnext.comnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-33769478083629039412006-08-01T15:39:10.000+01:002006-08-01T15:39:10.000+01:00If you used clone() in the body of the method then...If you used clone() in the body of the method then it could be done. Reflection might be another approach, though it would be a little messy.Kevinnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-65199504272235239942006-08-01T05:16:10.000+01:002006-08-01T05:16:10.000+01:00Sorry to reply 3 times in a row, but after giving ...Sorry to reply 3 times in a row, but after giving it some proper thought I don't understand how you can implement the method in the superclass and have it return an instance of the subclass?Eugenenoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-21843335768939475562006-08-01T02:04:58.000+01:002006-08-01T02:04:58.000+01:00You can change return types covariantly, like this...You can change return types covariantly, like this:<br /><br />public abstract class A {<br /> A withYear(int year) {<br /> // implementation<br /> }<br />}<br /><br />public class B extends A {<br /> B withYear(int year) {<br /> return super.withYear(year);<br /> }<br />}<br /><br />B b = new B().withYear(2006);<br /><br /><br />It's messy but it works.Keith Leanoreply@blogger.com