Say I have a class like so:
abstract class Something {}
And it has a hierachy with classes extending it:
class FirstSomething e
You can make the A class generic by specifying that the type can be Something
or one of its subclasses (I've made the setSomething
method abstract but you can provide an implementation if you want).
abstract class A {
public abstract void setSomething(T something);
}
Then force the type T to be the specific class you want in your subclasses.
class B extends A {
@Override
public void setSomething(FirstSomething something) {
//Set the something
}
}
class C extends A {
@Override
public void setSomething(SecondSomething something) {
//Set the something
}
}