Re: Advanced Java Generics syntax question
Christopher Sahnwaldt <[email protected]> Wed, 12 Mar 2008 02:52:42 +0100
| Newsgroups | gmane.comp.windows.devel.java.advanced |
|---|---|
| Message-ID | <[email protected]> |
I know this is an old post, but... java.util.List<T> declares the method contains(Object), not contains(T). That's why contains(String) does not override any method and is not called in your example. When the collections were generified, only methods that add elements to collections (add, set, put...) got generic parameters, all others stuck with Object. Maybe this was supposed to improve backwards compatibility? I don't know. It has bitten me a couple of times - you change the generic type of a collection and expect the compiler to find all method calls that you have to adapt, but it doesn't... On Sat, Feb 16, 2008 at 12:20 AM, Dilum Ranatunga <[email protected]> wrote: > Note that doing this doesn't allow you to overload the super class's > method elegantly. For example: > > public void testAnonymousSubclass() { > List<String> list = new ArrayList<String>() { > public boolean contains(String candidate) { > if (candidate == null) { > return super.contains(null); > } > for (String element : this) { > if (candidate.equalsIgnoreCase(element)) { > return true; > } > } > return false; > } > }; > list.add("a"); > assertFalse(list.contains("A")); > // contains(Object) was called; not contains(String). > > } > > > On Feb 15, 2008 3:19 PM, Brian Maso <[email protected]> wrote: > > Yup, that did it. That was driving me crazy! Thanks! > > > > Brian Maso > > > > > > At 01:08 PM 2/15/2008, you wrote: > > >Brian Maso wrote: > > >>I want to define an anonymous subclass of a generic class -- how do I > > >>set the parameter type? > > >> > > >>Here's a non-anonymous example of a subclass binding the superclass > > >>type variable: > > >> > > >>public LinkedListOfStrings extends LinkedList<String> { > > >> ... > > >>} > > >> > > >>That's no problem. Now let's say I want to do the same thing but with > > >>an anonymous inner class: > > >> > > >>class Outer { > > >> > > >> public void someMethod() { > > >> > > >> LinkedList<String> lls = new LinkedList() { <-- how do I > > >>indicate the superclass is LinkedList<String>? > > >> public void add( String s ) { ... } > > >> ... > > >> } > > >> > > >> } > > >> > > >>} > > > > > >Sorry, previous post had following line transposed. > > > > > >LinkedList<String> lls = new LinkedList<String>() { > > > public void add( String s ) { ... } > > >}; > > > > > >=================================== > > >This list is hosted by DevelopMentor(R) http://www.develop.com > > > > > > >View archives and manage your subscription(s) at http://discuss.develop.com > > > > =================================== > > This list is hosted by DevelopMentor(R) http://www.develop.com > > > > > > View archives and manage your subscription(s) at http://discuss.develop.com > > > > =================================== > This list is hosted by DevelopMentor(R) http://www.develop.com > > View archives and manage your subscription(s) at http://discuss.develop.com > =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com