sealed class StockLabel : Label() {
object OutOfStockLabel : StockLabel()
}
I know sealed class in kotlin is implicitly abstract and we will get compile error if doing so. But I saw the usage of the code above, the 'OutOfStockLabel' is declared within the 'StockLabel' sealed class also with 'StockLabel()', the 'StockLabel()' I think should be the type for 'OutOfStockLabel' but is it here for initialization or?
Need some help to understand the code case here.
CodePudding user response:
Although the syntax looks similar, this code:
object OutOfStockLabel : StockLabel()
is the syntax for defining a class, not creating an instance of the abstract class. You are declaring a subclass (an object
in this case) whose primary constructor (which is implicit for an object
) calls the abstract class's constructor. This is permitted for abstract classes. If you couldn't, it would be impossible to create any subclasses of an abstract class.