JAVA Inheritance Generics and Casting.
- by James Moore
Hello,
I have two classes which both extends Example.
public class ClassA extends Example {
public ClassA() {
super("a", "class");
}
....
}
public class ClassB extends Example {
public ClassB() {
super("b", "class");
}
....
}
public class Example () {
public String get(String x, String y) {
return "Hello";
}
}
So thats all very well. So suppose we have another class called ExampleManager. With example manager I want to use a generic type and consequently return that generic type. e.g.
public class ExampleManager<T extends Example> {
public T getExample() {
return new T("example","example"); // So what exactly goes here?
}
}
So where I am returning my generic type how do i get this to actually work correctly and cast Example as either classA or classB?
Many Thanks