I have a interface called ABC. I also have an abstract class PQR and there are 3 classes which extend this abstract class.
For example:
abstract class PQR {
//...
}
interface ABC {
//other methods....
PQR returnRightClass();
}
class X extends PQR {
}
class Y extends PQR {
}
class Z extends PQR {
}
Now there are classes which implement the ABC interface. So what should I do to get the right class from X, Y, and Z?
There is nothing wrong with returning an abstract class, though it is recommended to use interfaces as much as possible. If you can make
PQRan interface, then do so. But if not, no worry.Just make sure each implementation of
returnRightClass()in each class implementingABCreturns a proper instance ofPQR(x,y or z).EDIT
To answer your question:
Or
Or