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

  • Home
  • SEARCH
  • 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 8838173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:55:22+00:00 2026-06-14T09:55:22+00:00

I have outer generic class which has some inner class, for which I’d like

  • 0

I have outer generic class which has some inner class, for which I’d like to use the generic type of outer class.

However, I don’t understand how to use generic parameters correctly.

Example:

class Outer<E extends CharSequence>{
   class Inner extends ArrayList<E>{
   }

   void func() {
      ArrayList<CharSequence> al = new ArrayList<CharSequence>();
      al.add("abc");    // OK
      CharSequence a = al.get(0);   // OK

      Inner in = new Inner();
      in.add("abc"); // Error: The method add(E) in the type ArrayList<E> is not applicable for the arguments (String)
      CharSequence b = in.get(0);   // OK
   }
}

How can I declare inner class to use same generic type of outer class?
Thanks

EDIT + Solution:

Finally I achieved what I wanted, here’s example result:

abstract class MyGenericClass<E extends CharSequence>{
   class TheList extends ArrayList<E>{};

   TheList list = new TheList();
}

final class ClassInstance extends MyGenericClass<String>{
};

public class Main{
   public static void main(String[] args){
      ClassInstance c = new ClassInstance();

      c.list.add("abc");
      String s = c.list.get(0);
   }
}

My requirement was to have generic class, and also have generic container in it, which would use same type parameter as its parent class.

Note: CharSequence/String are example parameters of use, my real usage is different.

  • 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-14T09:55:23+00:00Added an answer on June 14, 2026 at 9:55 am

    How can I declare inner class to use same generic type of outer class?

    Inner is already using the same generic type as Outer. The problem is that func is not using it, it is using CharSequence.

    As SLaks explained func will do funky stuff in all cases except when it is called on an Outer<CharSequence>: e.g. when called on Outer<String> it will add a CharSequence to an ArrayList<String>.

    Now, if func already had an object of that concrete type:

    void func(E e) 
    {
        Inner in = new Inner();
        in.add(e);
    }          
    

    It would do the right thing. So the compiler allows this.

    It seems you tried to create a generic question from some specific issue you are facing. If you described what you are trying to do and failing you will have better chance of resolving it here.

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

Sidebar

Related Questions

I have some javascript code with do while outer loop and switch inner loop,
I have the following code: class outer { struct inner { int var1; int
I have code like this (simplified): def outer(): ctr = 0 def inner(): ctr
I have class outer: public base { public: class inner { foo(); } }
I have Outer DIVS like this. <Input id =SaveMe value=Click type=submit > </Input> <DIV
I have an outer class, Outer, and an inner class, Inner. For testing a
I have an outer div with a few inner divs like this: HTML: <div
I have outer div and inner div. I need to place inner div at
I have an outer div, and inside of that, I have an inner div
I have an outer div that is 800px wide. I have an inner div

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.