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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:10:57+00:00 2026-06-07T17:10:57+00:00

Is it possible, within Java generics, to store multiple subtypes at the same time?

  • 0

Is it possible, within Java generics, to store multiple subtypes at the same time?

I was under the impression when you declare your container, the generic parameter has to be concrete (rather than an interface or abstract superclass), so this only leaves using a non-abstract superclass?

  • 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-07T17:10:59+00:00Added an answer on June 7, 2026 at 5:10 pm

    I guess this code sample will show you the basics about generics:

    abstract class A {
        protected String s;
        //getters and setters...
    }
    
    class B extends A {
        public B() { s = "I'm B object"; }
    }
    
    class C extends A {
        public C() { s = "I'm C object"; }
    }
    
    public class Main {
    
        public static void main(String[] args) {
            List<A> listA = new ArrayList<A>();
            listA.add(new B());
            listA.add(new C());
            List<B> listB = new ArrayList<B>();
            listB.add(new B());
            listB.add(new C()); //compile error: datatype mismatch
    
            printListA(listA);
            printListA(listB); //compile error: datatype mismatch
            printListAExtended(listA); //this compiles and executes right
            printListAExtended(listB); //this compiles and executes right
        }
    
        //this method must receive a List<A> object
        public static void printListA(List<A> listA) {
            for(A a : listA) {
                System.out.println(a.getS());
            }
        }
    
        //this method must receive a List<A or some class that implements A> object
        //that's why List<A>, List<B> or List<C> or any other List<anything implementing A>
        //can be passed here
        public static void printListAExtended(List<? extends A> listA) {
            for(A a : listA) {
                System.out.println(a.getS());
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title suggests is it possible to execute another java app from within
Is it possible to get a reference to this from within a Java inner
Is it possible with Java (and any widget toolkit: AWT/Swing/SWT/...) to render widget within
Is it possible to run Apache Pig jobs from within a Java application, without
Possible Duplicate: What is the garbage collector in Java? Within my program, I have
Possible Duplicate: How can I set the umask from within java? How do you
Is it possible to use Eclipse's classpath variables within in Java code? I have
Possible Duplicate: Use a variable within a variable? Java Hi this is a general
Is it possible to handle POSIX signals within the Java Virtual Machine? At least
Within Java code it is possible to have numeric literals that use a suffix

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.