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 8380389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:26:46+00:00 2026-06-09T16:26:46+00:00

I am a little confused with something. I have a class where its not

  • 0

I am a little confused with something.

I have a class where its not a collection, but it does refer to generic objects:

    public class XClass<E extends AnInterface>{

        E instanceobject;

        public void add(E toAdd){}
    }

    public interface AnInterface{}

    public class A implements AnInterface{}

    public class B implements AnInterface{}

I believe I read somewhere that <? extends AnInterface> is to be used (when declaring an instance of XClass) if you want multiple subtype-types in the generic object at the same time, whereas <T extends AnInterface> would only allow you to have a single type of subtype in the generic class at once?

However, I can just use:

    XClass<AnInterface> xc = new XClass<AnInterface>();

    A a = new A();
    B b = new B();

    xc.add(a);
    xc.add(b);

and this way I can pass in multiple subtypes of Supertype to the generic class……

I am not seeing the purpose of using “?” and is there anything wrong with using the Interface as the generic parameter?

  • 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-06-09T16:26:47+00:00Added an answer on June 9, 2026 at 4:26 pm

    The reason why you can add objects of both type A and B is due to the fact that you parametized your XClass with the interface, so there is nothing wrong with adding two different classes that implement that interface.

    If, on the other hand, you had defined XClass as:

    XClass<A> xc = new XClass<A>();
    

    then the expression xc.add(b); would give a compilation error, since all the objects added must have the same type as was declared, in this case, A.

    If you declare you xc as, for instance:

    XClass<? extends AnInterface> xc = new XClass<AnInterface>();

    Then it’s not legal anymore to add a or b, since the only thing we know is that xc is of some unknown but fixed subtype of AnInterface, and there is no way to know if that unknown type is A or B or anything else.

    But let’s say you’re writing a method to accept a XClass type that you can iterate over the elements that were added before. Your only restriction (for the sake of the example), is that the items extend AnInterface, you don’t care what the actual type is.

    You can declare this method like:

    public static void dummyMethod(XClass<? extends AnInterface> dummy){
    //do stuff here, all the elements extend (implement in this case), AnInterface, go wild.
    }
    

    And now you can pass into this method anything like XClass<A>, XClass<B> or XClass<AnInterface>, and it will all be valid.

    Keep in mind that you can’t add to the object you pass, for the same reason above. We don’t know what the unknown type is!

    public static void dummyMethod(XClass<? extends AnInterface> dummy){
    //do stuff here, all the elements extend (implement in this case), AnInterface, go wild.
        dummy.add(new A()); //you can't do this, we have no idea what type ? stand for in this case
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a little confused about how C++ handles includes. I have something like: typedef
I'm just a little confused here. Does the existing kmeans function in matlab have
I'm a little confused about something. I wrote an app and tested it on
I am a little bit confused on something, see I am doing an Ajax
I am a little confused about Accept-Encoding . I have Web Service which would
I'm a little confused by the structure of the Node class in OpenJDK's implementation
I'm getting a little confused with using Absolute and Relative positioning. Basically I have
I'm a little confused about how to do something that I thought would be
I've been working with the SystemTray in WP7, but I'm a little confused if
I am little confused with this code am looking for somenthing similar i have

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.