Results 1 to 2 of 2
  1. #1
    Sven is offline Member
    Join Date
    Dec 2008
    Location
    Enschede, Overijssel, the Netherlands
    Posts
    19
    Rep Power
    0

    Default Generics and subclasses

    Hi all, I was wondering how to solve the following problem with generics:

    I have three classes:
    - class A
    - class B extends A
    - class C => contains a LinkedList of A's, like so:

    Java Code:
    class C {
        ...
        LinkedList<A> list = new LinkedList<A>();
        ...
        public LinkedList<A> getList() {
             return list;
        }
    }
    Now, in another class, I'm using the getList() method to retrieve the list of A's. Now I would like that, while iterating over the list, there might be A objects OR B objects in the list, so I would like to cast them to the appropriate type, but this is not possible. Now I know this should not be possible because LinkedList<B> is NOT a subtype of LinkedList<A>. Normally you would solve this by declaring list as a LinkedList<? extends A>, but this is not possible in creation, only in definition (it gives compiler errors if I try to do so).

    At the moment, I am using a @SuppressWarnings("unchecked") combined with a LinkedList of a non-generic type (contains Objects, and I cast them in an iterator loop). This is kind of ugly and my intuition suggests that it should be possible to use generics here in some way, but does anyone know how?

    Thanks in advance!
    Last edited by Sven; 12-16-2008 at 11:32 AM.

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default List container sub-classed generic

    Try this:

    Java Code:
    package test;
    
    import java.util.ArrayList;
    
    public class GenericTest {
    	public static void main(final String[] args) {
    		final GenericTest instance = new GenericTest();
    		instance.run();
    	}
    
    	public void run() {
    		final ArrayList<A> list = new ArrayList<A>();
    		list.add(new A());
    		list.add(new B());
    		for (final A efA : list) {
    			if (efA instanceof B) {
    				System.out.println("It's a B!");
    			}
    			else {
    				System.out.println("It's just an A...");
    			}
    		}
    	}
    
    	class A {}
    
    	class B
    	    extends
    	    A {}
    }
    Here's the output I got:

    It's just an A...
    It's a B!

    I think you just need to use the instanceof operator...

    -Steve

Similar Threads

  1. Main class and subclasses
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 11-19-2008, 06:16 PM
  2. Can I use annotations in superclass, and .cfg.xml files in subclasses in EJB3
    By narayanrec in forum Enterprise JavaBeans (EJB)
    Replies: 4
    Last Post: 01-06-2008, 02:40 AM
  3. Replies: 0
    Last Post: 01-02-2008, 04:24 AM
  4. help needed with methods in subclasses
    By uncopywritable in forum New To Java
    Replies: 4
    Last Post: 08-01-2007, 01:47 PM
  5. Hibernate subclasses
    By Ed in forum JDBC
    Replies: 2
    Last Post: 07-02-2007, 04:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •