Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By JosAH

Thread: Generic Container

  1. #1
    Registred is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Generic Container

    Hey,
    I want to create a abstract class which defines a list of the type of it's child class.

    For example:
    I have an abstract class A which has a list of the class extending it.
    I have a class B which extends class A and therefore has a list of type B.

    It's important that the list can only hold the type of it's own class. I want to define it in the abstract base class because I don't want to define it in every single subclass.

    How can I achieve this best? Is this possible with generics?
    Greetings!

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,909
    Rep Power
    5

    Default Re: Generic Container

    You can do it. But why would you want too? And what have you tried?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Registred is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Generic Container

    Thanks for your answer,

    what I try is to create is a hierarchical structure of classes. Every class should be able to hold a list of it's own type or every class deriving from it (polymorphism). (Yes, I've made a little mistake in my first post).
    Generic Container-2nquqg7.jpg

    In this case, A is my abstract class defining the list of childs.
    Now, B should have a list of B, C should have a list of C and so on, ...

    I tried the following in class A:

    List<? extends A> childs;
    but the problem is, that C would also be able to hold an object of type B.
    What i need is something like:

    List<DerivingClass> childs;
    so that C actually has a list of C, and D has a list of D, even if it extends C and not A.

    Greetings!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Generic Container

    I'm no expert in Generics, but I think you need something like
    Java Code:
    public abstract class AbstractSuper<T extends AbstractSuper> {
    
      protected List<T> list = new ArrayList<T>();
    }
    
    class SubA<T extends SubA> extends AbstractSuper<T> {
    }
    
    class SubB<T extends SubB> extends AbstractSuper<T> {
    }
    
    class SubSubC<T extends SubSubC> extends SubA<T> {
    }
    
    class SubSubD<T extends SubSubD> extends SubB<T> {
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Registred is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Generic Container

    Thanks for your answer!

    This would be a solution theoretically. But every time I want to instance an object of type SubA for example I'd have to write

    SubA<SubA> sub = new SubA<SubA>();
    This is kind of weird, especially when giving the code to another person.
    Is there a way to make this more clear when instancing the object? Maybe by forcing the programmer of a sub-class to implement a list of his own type by an interface or something?
    If there's no better way I'll go with this one.

    Greetings!

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: Generic Container

    Actually you only have to write

    Java Code:
    SubA<SubA> sub = new SubA<>();
    And yes, over-using generics can lead to quite ugly and at times hard to follow code. Its something we have to live with.

    I think you can put another layer of abstraction on top, like:

    Java Code:
    public abstract class SubABase<T extends SubABase> extends AbstractSuper<T> {
    }
    
    class SubA extends SubABase<SubA>{
    }
    but I may be mistaken; generics with inheritance give me a head ache.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Generic Container

    Quote Originally Posted by gimbal2 View Post
    I think you can put another layer of abstraction on top, like:

    Java Code:
    public abstract class SubABase<T extends SubABase> extends AbstractSuper<T> {
    }
    
    class SubA extends SubABase<SubA>{
    }
    Then a subclass (or a sub-sub-class) of SubA would have a List<SubA>. For a second generation of inheritance, it would be necessary to extend SubABase. And then that wouldn't be a subclass of SubA.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: Generic Container

    Quote Originally Posted by DarrylBurke View Post
    Then a subclass (or a sub-sub-class) of SubA would have a List<SubA>. For a second generation of inheritance, it would be necessary to extend SubABase. And then that wouldn't be a subclass of SubA.

    db
    Head... hurts...
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Generic Container

    Quote Originally Posted by gimbal2 View Post
    Head... hurts...
    Can I offer you a generic painkiller?
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,727
    Blog Entries
    7
    Rep Power
    21

    Default Re: Generic Container

    Quote Originally Posted by gimbal2 View Post
    Head... hurts...
    Don't pay any attention to it; sometimes Darryl has this behaviour when he forgets to take his pills; when we ignore it he goes back to normal soon ...

    kindest regards,

    Jos ;-)
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with adding a Container within a Container
    By JoKeR313 in forum New To Java
    Replies: 4
    Last Post: 01-02-2011, 05:59 AM
  2. Java Generic Container Question - please help!!!
    By zhoujackji in forum New To Java
    Replies: 3
    Last Post: 11-13-2010, 12:12 PM
  3. Replies: 3
    Last Post: 04-22-2009, 12:01 AM

Posting Permissions

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