Taranfx
Taranfx

Reputation: 10437

Generics for arguments in overriden method

Am I using generics in the wrong way? Sorry I'm new to generics.

public interface Stream {
 public <T extends InputStream> void read(T in);
}

public class StreamReader implements Stream {
  @Override
 public <T extends InputStream> void read(T in) {
   ByteInputStream bis = (ByteInputStream) in;
   ...
}

Upvotes: 0

Views: 31

Answers (2)

Niraj Patel
Niraj Patel

Reputation: 2208

You can change your method from:

public <T extends InputStream> void read(T in);

To:

public <T extends InputStream> void read(<T extends ByteInputStream> in);

Upvotes: -1

Harmlezz
Harmlezz

Reputation: 8068

I think you are in search of this:

public interface Stream<T extends InputStream> {
    public void read(T in);
}

public class StreamReader implements Stream<ByteArrayInputStream> {

    @Override public void read(ByteArrayInputStream in) {
        ...
    }
}

Upvotes: 4

Related Questions