I have a class ClassA with some methods returning items:
class ClassA {
ClassA item(...) {...}
Vector<ClassA> list(...) {...}
}
These methods process an input and generate on the fly these kind of items. Now imagine I extend this class:
class ClassB extends ClassA {
...
}
I do not override item() or list() methods in any case, but I’d like them to return a ClassB item (or vector of) without casting on the return (which is my current solution now). Is there any way to do so? Thanks in advance.
I would create a third class (ClassC) to hold your ClassA or ClassB items, that is parametrized with a generic type like this:
Now you can instantiate ClassC with either a ClassA or ClassB type and never have to worry about class casting.