Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: Hairy generics question Date: Tue, 21 Feb 2012 09:24:11 -0800 Organization: albasani.net Lines: 58 Message-ID: References: <3c65271e-a388-49c9-bcc6-ca3bf274e74f@e27g2000vbu.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net +JtfMt1VlLIGuynoFgi96wuTViGdqSJWJXhY+zEkY55n248J+FwoV8t/jPyOKsX8RpzG1avRDJHlBnNilk2dDCUi7WgRQ/X3bDm9M5fHb6TH/oaQmqObqLUL+UWVCKQ2 NNTP-Posting-Date: Tue, 21 Feb 2012 17:24:13 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="848eaEVHNT7e0YcCOrHcmWAQW2/8vSat5+s6nbp3LrGuEsseBIMJiWn3mQ4rDrHTGij9d7YAyyHpSueFL/nPKGcHR2/aorKsCV4u1FoUDQjCzaSzyo04ZZNgov0GdzNR"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 In-Reply-To: <3c65271e-a388-49c9-bcc6-ca3bf274e74f@e27g2000vbu.googlegroups.com> Cancel-Lock: sha1:7y2abIvmkbSPNUl1vWsJj4mxNGM= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:12234 On 02/21/2012 06:30 AM, sclaflin wrote: > I have a number of sets of matched parameterized classes: a bean, a > presenter, and a view. > The presenters and views have parallel inheritance hierarchies (where > T is the bean type, P the presenter type, and V is the view type): > > public class CompA extends CompItem > public class CompB extends CompItem > > public class CompAView extends CompItemView > public class CompBView extends CompItemView > > public class CompItemView P extends AbstractCompItem, > V extends CompItemViewInterface V>> > extends AbstractCompItemView > implements CompItemViewInterface > > public abstract class CompItem P extends AbstractCompItem, > V extends CompItemViewInterface P, V>> > extends AbstractCompItem > implements CompWidget > > Many of the views are the same, so I wanted to just use the base view > class instead of creating a separate view class for each presenter. > So I tried: > > public class CompA extends CompItem > > But I get an error that says "The type CompItemView is not a valid > substitute for the bounded parameter CompItemViewInterface> of the type CompItem. > Why can I use a class that extends CompItemView for the V type, and > not CompItemView itself? Your generics assertions have 'P' extend an 'AbstractCompItem' that depends on 'P'. This type of generics circularity has a place, but it's often the result of confusion also. Which is it for you? Why does 'P' extend its own type? (Similarly for 'V'.) As for covariance, you need to study the generics tutorial and Angelika Langer's site. The combination is likely what's killing you. Remember that 'Foo extends Bar' does not imply (cannot imply) that 'Baz extends Baz'. If you remove the circularity - if feasible, but please do explain why it's there at all - and do your covariance correctly your problem should vanish. I think perhaps you've overcomplicated your generics. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg