sealed class Expr
data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()
Like the example code, why do we put the parentheses for the case of sealed classes,
e.g. like : Expr(), unlike normal classes where we would write : Expr ?
Doesn’t Expr() mean instantiating the class? If this is the case, it would contradict to the sentence from the documentation
A sealed class is abstract by itself, it cannot be instantiated directly and can have abstract members.
I also don’t understand why here
Sum(val e1: Expr, val e2: Expr) : Expr()
for properties e1 and e2 we write : Expr while for class Sum we write : Expr()
How should I understand this, and what’s happening under the hood?
Whenever a class inherits from another it also needs to call the constructor of it’s parent. This is also true for sealed classes.
This is not true if the derived class has a primary constructor. In that case you need to specify what constructor of the base class you want to call so you have to add the parenthesis.
To declare an explicit supertype, place the type after a colon in the class header:
open class Base(p: Int)
class Derived(p: Int) : Base(p)
If the derived class has a primary constructor, the base class can (and must) be initialized right there, using the parameters of the primary constructor.
If the derived class has no primary constructor, then each secondary constructor has to initialize the base type using the super keyword, or to delegate to another constructor which does that. Note that in this case different secondary constructors can call different constructors of the base type:
The primary constructor is the constructor that comes before the braces. So
class Foo /* primary constructor */ : BaseClass() {
}
The only way not to have a primary constructor is to add a secondary constructor
class Foo : BaseClass {
constructor(): super() {}
}
You can have as many secondary constructors as you want and you can also combine secondary constructors with a primary. In this case each secondary constructor has to either call another secondary constructor or the primary constructor