Reputation: 11
lets say you got a parent class called block that has a lot of child classes and has the abstract method called rotate that should give you back a different child of block. what should the abstract method "give back" if its always a specific other child object ?
i tried working around it but it unfortunately doesn't work with other options trying to just change the parameter i needed to in the method implementation of the child classes.
I have tried looking up any solutions or examples for this but i'm not quite sure for what to actually search.
lets say
public class Block{
abstract public void rotate();
}
but instead of void what should i do to give back a different object extending block. if i got the public class T extends Block{} and the method rotate should give back the object from public class Ttilt extends Block{}.
Upvotes: 1
Views: 38
Reputation: 662
The solution should implement generics. This would look like the following:
public abstract class Parent<T extends Parent<T>> {
...
abstract T method();
...
}
Your child class would look like:
public class ChildA<ChildA> {
...
}
This will let you call method()
through any of the child classes and that method will return an instance of that specific child class.
Upvotes: 0