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.

Pointer Null
  • 39,597
  • 13
  • 90
  • 111

3 Answers3

4

That is fundamentally unsafe.

What would happen if I make an Outer<StringBuilder>?
You would end up adding a String to an ArrayList<StringBuffer>.

If you have a collection of <E extends CharSequence>, the only type-safe thing you can put in that collection is an instance of E (or a class that extends E, or null).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Actual types are not relevant here, String/Charsequence are examples, I work with other class hierarchy in my project, where I want to make this work. – Pointer Null Nov 15 '12 at 19:13
  • @mice: That's still unsafe. `String` is just an example. See my last paragraph. – SLaks Nov 15 '12 at 19:21
2

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.

Community
  • 1
  • 1
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
-3
 class Inner extends ArrayList<CharSequence>{
 }
irreputable
  • 44,725
  • 9
  • 65
  • 93