Re: Advanced Java Generics syntax question

Dilum Ranatunga <[email protected]> Fri, 15 Feb 2008 17:20:11 -0600
Newsgroups gmane.comp.windows.devel.java.advanced
Message-ID <[email protected]>
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®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com