R
R
RapperOfGods2016-10-05 19:38:49
Java
RapperOfGods, 2016-10-05 19:38:49

Is it possible in java to inherit a generic class from another generic class?

Is it possible to inherit a generic class by another generic class, so that the derived class can only accept as a parameter the descendants of a particular class?
Below is the crux of the issue.

class class1<T> {
    ...
}

class class2<T extends SomeType> extends class1<T> {
    ...
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Kuznetsov, 2016-10-09
@DarkRaven

I have created two classes:

public class Class1<T> {

  public void Run(T another){
    //TODO Do something  with another
  }
  
}

and
public class Class2<T extends Synker> extends Class1<T> {

}

then I do this:
Class2<Synker> cl = new Class2<>();
cl.Run(new Synker());

Everything compiles fine. What is your type, SomeType? From my example, is this what you needed?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question