简体   繁体   中英

How do I inherit from a generic class in java?

I have a class like following

public class Foo<idType extends WritableComparable<idType>, EData extends Writable> {
  public Foo();

  public Foo(idType foo; idType bar){
  this.foo = foo;
  this.bar = bar;
  }
  private idType foo;
  private idType bar;

}

Now one of the usage of this class is like following:

elist = new ArrayList<Foo<StringType, EmptyType>>();

So this works just fine:

Now I want to extend this class to add one more field

private String foobar;

Now, basically an instance of this would be have three fields.

Two of them

   foobar.foo //base class
   foobar.bar //base class
   foobar.foobar // new variable added

Now, my usage is still the same:

 elist = new ArrayList<FooBar<StringType, EmptyType>>();

I tried a simple extension:

 public class Foobar extends Foo{
 private String foobar;
 public FooBar(String foobar){this.foobar = foobar;}

}

But when I use

I get an error:

 elist = new ArrayList<FooBar<StringType, EmptyType>>();
ArrayList<FooBar><StringType,EmptyType>> cannot be resolved to a type

If you want to let the user specify the types to your subclass, specify the same type parameters, and pass them on to the base:

public class FooBar <idType extends WritableComparable<idType>, EData extends Writable>
    extends Foo<idType, EData>
{ 
    ...
}

If you want to let the user only specify one of those types, you can do that, eg you want to force Integer for idType :

public class FooBar <EData extends Writable>
    extends Foo<Integer, EData>
{ 
    ...
}

If you want to only use specific types for the base, same idea:

public class FooBar
    extends Foo<Integer, Something>
{ 
    ...
}

You can even add a type:

public class FooBar <idType extends WritableComparable<idType>, EData extends Writable, AnotherType>
    extends Foo<idType, EData>
{ 
    private AnotherType x;
    ...
}

The point is, you specify your own parameter types in the subclass in any way you see fit, and you can pass those types to the base as long as they are compatible types.

Edit: Responding to a comment on the question above, you do have to specify constraints on the FooBar type parameters that match constraints on the base Foo . For example, the following is not sufficient:

public class FooBar <idType, EData>
    extends Foo<idType, EData> // <-- will fail to compile
{ 
    ...
}

This will lead to the following compilation errors:

type parameter idType is not within its bound
type parameter EData is not within its bound

This is because Foo expects types that extend WritableComparable<idType> and Writable , respectively, but the above erroneous declaration of FooBar attempts to pass types that do not meet those constraints as type parameters to Foo .

Your error, by the way, as posted, does not appear to match your code and has an extra > at the end. It appears you made a typo when copying and pasting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM