JAVA Inheritance Generics and Casting.

Posted by James Moore on Stack Overflow See other posts from Stack Overflow or by James Moore
Published on 2010-04-15T20:29:43Z Indexed on 2010/04/15 20:33 UTC
Read the original article Hit count: 285

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about generics