I am implementing Page Object Model for mobile application automation framework. For readability for each page I want to have 2 classes – Page itself having properties and basic functions only and Steps to have all other methods.
I want to create abstract class with some abstract methods and then force all classes that inherit from this class to have implementation only for these needed overridden methods, no more.
I tried to create classes as follows:
abstract class A() {
abstract fun foo()
abstract fun bar()
}
final class ImplA(): A() {
override fun foo() { println("foo") }
override fun bar() { println("bar") }
fun baz() { println("baz") } //not wanted
}
Then creating all object of class ImplA()
casted directly to abstract class A()
:
fun main() {
val tmp = ImplA() as A()
tmp.foo()
tmp.bar()
tmp.baz()
}
In such case I got exception when calling tmp.baz()
. I see 2 problems with this solution:
- I need to remember to cast every object to abstract class.
- Nothing prevents defining other (non overridden) methods in classes.
Is there any other way of preventing defining non overridden methods in classes?