Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6539081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:48:51+00:00 2026-05-25T10:48:51+00:00

this question is partially related to my last question . I have a generic

  • 0

this question is partially related to my last question.

I have a generic class representing a collection of generic objects:

public interface MyObject<N extends Number>{}

public interface MyCollecion<N extends Number> {
    public foo(MyObject<N> obj)
}

In my design these collections of objects are constructed by a client class (let’s call it CollectionBuilder) through an abstract class approach:

public interface AbstractCollectionFactory {
    public abstract <N extends Number> MyCollection<MyObject<N>> createCollection(String collectionType);

}

Generic collections should be constructed this way:

public class CollectionBuilder{
    AbstractCollectionFactory f;
    public void buildDoubleCollection(){

         MyCollection<MyObject<Double>> c = f.<Double>createCell("MyType");
    }
    public void buildIntegerCollection(){...}
}

Ok.Since here all ok. CollectionBuilder is aware of what is the generic concrete type to specify (Double in this case) and can build correctly. I can compile this with no warning and all should work fine.

Now I have a question related both to generics and the design of my program.

I have another class in my application that need to use the collection built by CollectionBuilder (let’s call this class UserClass).
The UserClass:

  1. Doesn’t need to know of which particular concrete type are its collection (MyCollection<MyObject<Double>> or MyCollection<MyObject<Integer>>).
  2. perform some operations on these collections invoking some methods defined in MyCollection interface.
  3. I would not want to add a generic type to it.

In the situation described, is that a bad idea do not parametrize the generic class MyCollection insied UserClass?

public UserClass{
     MyCollection a;  //warning 
     MyCollection<?> b; //no warning

     public method(MyObject o){  //warning
          a.foo(b); //compile       
     }
     public method2(MyObject<?> o){
          b.foo(o); //error do not compile
     }
}

Java compiler always protest with a warning if I don’t specify the generic parameter. Anyway from inside UserClass I don’t know the concrete parameter (and I would like to don’t know it) and even declaring it with “?” don’t allow me to call method foo on MyCollection.

So the question are:

  1. not parameterizing a reference to a generic type is always a bad thing?
  2. if answer at 1 is NO, is this a right situation for not doing it?
  3. if answer at 1 is YES, how can I use MyCollection method from inside UserClass without knowing their generic types?
  4. Is mine a bad design ?

I hope to have been clear. I’ve be struggling on this problem from days, and I have no idea. Please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:48:52+00:00Added an answer on May 25, 2026 at 10:48 am
    1. According to the Java compiler, yes. And this answer contains a lot of good points as to why. Personally I disagree in some situations, especially where the use of Class is concerned (like for instance, if you have a Map<Class, Object>). In such cases having to always tack <?> on to the end of “Class” just feels like needless tedium, and does not make the code any more readable.

      The idea of having Class be parameterized based upon what type of Object it is associated with has always felt a bit dubious in the first place, to me. The point of Class is to have a common interface that can be used to obtain information about any Object (ala reflection), regardless of its type.

      But I digress. According to the Java compiler and the implementors of generic types, you should always parameterize your reference, even if you are only using <?>.

    2. Not applicable. Though you can always not declare any type information, and then use @SuppressWarnings("unchecked") along with an explicit cast to make the compiler happy.

    3. See Bohemian’s answer.

    4. There’s not really enough information to say. Though off the top of my head, I’m not sure what the actual use-case would be for having “a generic class representing a collection of generic objects”. Why not just use the collections directly?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a constructor question for C#. I have this class: public partial class
Partially related to an earlier question of mine , I have a system in
This question os partially a Drupal question and partially a SQL question. I have
This question in partially related to my previous post on this subject. I would
I guess my question derives partially from this question: std::ifstream buffer caching. I have
My question is partially inspired by this article written by Eric Lippert: http://blogs.msdn.com/ericlippert/archive/2009/10/19/what-is-this-thing-you-call-thread-safe.aspx Using
I got a CSS question related to this fiddle: http://jsfiddle.net/r584e/ Here the relevant screenshot
This question follows on from Assembly does not allow partially trusted callers when using
This question is related to c#.The scenario is that when i click the button
I am working in asp.net MVC2 application and I have question related to design

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.