In C#:
sealed
virtual
(or abstract
)sealed
C#
public class BaseClass // You CAN extend this
{
public string Property1 => "You CANNOT override this";
public virtual string Property2 => "You CAN override this";
public virtual string Method() => "You CAN override this";
}
public class OpenClass : BaseClass // You CAN extend this
{
public sealed override string Property2 => "You CANNOT override this";
public override string Method() => "You CAN override this";
}
public sealed class ClosedClass : BaseClass // You CANNOT extend this
{
public override string Property2 => "You CANNOT override this";
public override string Method() => "You CANNOT override this";
}
In Kotlin:
final
(sealed) unless marked open
(or abstract
)open
class are implicitly open but can be closed with final
Kotlin
open class BaseClass { // You CAN extend this
val property1 = "You CANNOT override this"
open val property2 = "You CAN override this"
open fun method() = "You CAN override this"
}
open class OpenClass : BaseClass() { // You CAN extend this
final override val property2 = "You CANNOT override this"
override fun method() = "You CAN override this"
}
class ClosedClass : BaseClass() { // You CANNOT extend this
override val property2 = "You CANNOT override this"
override fun method() = "You CANNOT override this"
}