I have a super class called PageObject, and then two subclasses called AlphaPage and BetaPage that inherit the PageObject.
A function “selectPage()” will return one of these pages, but the specific page to return will only be known at runtime.
What should the function’s return object be, then, that will avoid having to cast one of the subclasses to the function call’s return val?
The caller will have to cast if they want the specific type, given that the information is only known at execution time. You should return
PageObject, basically.Of course, if the caller doesn’t need anything specific to
AlphaPageorBetaPage– if they can just use methods (possibly abstract) declared onPageObject, then there’ll be no need to cast. That would be the ideal – use polymorphism to handle the differences. It really depends on what the caller needs to do though.