Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

generics

Thread view: 
VisionSet - 18 Dec 2005 17:37 GMT
I have an attribute:

List<ConcreteType> attr;

but want to return it as an interface type:

public List<IntfType> getMyTypes();

where ConcreteType implements IntfType

how do I do that?

TIA
--
Mike W
zero - 18 Dec 2005 17:46 GMT
"VisionSet" <spam@ntlworld.com> wrote in news:N7hpf.47909$uR.8369@newsfe7-
gui.ntli.net:

> I have an attribute:
>
[quoted text clipped - 11 lines]
> --
> Mike W

List<ConcreteType> is not a subclass of List<IntfType>.  In fact, there is
no relation between those two types, even though the container is the same
and the parameters are related types.  So, I don't think it's possible.  
Maybe there is a workaround, although I don't know one off the top of my
head.  Maybe if you give a concrete example of what you want to do someone
can suggest how to get it done.

Signature

Beware the False Authority Syndrome

Paul Bilnoski - 19 Dec 2005 16:25 GMT
> "VisionSet" <spam@ntlworld.com> wrote in news:N7hpf.47909$uR.8369@newsfe7-
> gui.ntli.net:
[quoted text clipped - 21 lines]
> head.  Maybe if you give a concrete example of what you want to do someone
> can suggest how to get it done.

They do have a relationship, the two containers are covariant types. You
can use covariant notation, or what java calls "capture" (the ? notation
in parameter expressions) to give you a list of as much as you can tell
of the items in the list.

If you get back a list of '? extends A' it's as good as a List<A> in
that you can do whatever you could to an A, but you also know that what
is in the list is not only instance of the base type A.

import java.util.List;
import java.util.ArrayList;

public class GT
{
  public static interface A { }

  public static class B implements A { public String toString() {
return "B"; } }

  public static List<B> bees;

  public static List<? extends A> asListOfA() { return bees; }

  public static void main(String[] args)
  {
    bees = new ArrayList<B>();
    bees.add(new B());
    bees.add(new B());

    List<? extends A> as = asListOfA();

    for (A a : as)
      System.out.println(a.toString());
  }
}

--Paul Bilnoski
Thomas Hawtin - 18 Dec 2005 18:55 GMT
> I have an attribute:
>
[quoted text clipped - 5 lines]
>
> where ConcreteType implements IntfType

If you could assign from List<ConcreteType> to List<IntfType>, additions
to the later could break the former reference. For instance:

   List<String> s = new ArrayList<String>();
   List<Object> o = s; // Does not work!
   o.add(new char[4]);
   String c = s.get(0); // What happens here???

So, either start with List<IntfType>, use List<? extends IntfType> or
List<T extends IntfType> within a generic method, constructor or instance.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

ricky.clarkson@gmail.com - 19 Dec 2005 01:00 GMT
Make the attribute List<IntfType>.  Simple.

One line replies suck, so this is the second line.
VisionSet - 19 Dec 2005 16:20 GMT
> > I have an attribute:
> >
[quoted text clipped - 16 lines]
> So, either start with List<IntfType>, use List<? extends IntfType> or
> List<T extends IntfType> within a generic method, constructor or instance.

Yes of course.
I was trying to avoid making a new List object but I've done this which is
still nice and clean:

private LinkedList<ConcreteType> attr;
public List<IntfType> getMyTypes() {
   return new ArrayList<List>(attr);
}

--
Mike W


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.