The title says it. Here is a small example:
class Parent {
const Parent();
}
class Child extends Parent{}
Does Child
in this case have a const
constructor implicitly, or do I need to define a const
constructor explicitly?
I found the answer by chance. I had a 3-level hierarchy of classes, and I defined a const
constructor to the topmost parent. I left the (first) child class without defining a constructor. On the second child, I tried to define a const
constructor, and I was notified with an error in the code - that its parent didn't have a const
constructor.
class Parent {
const Parent();
}
class Child extends Parent {}
class Grandchild extends Child {
const Grandchild(); // shows an error
}
TL;DR: child classes need to explicitly define a const
constructor, even if their parents already have one.
const
constructor means that the object can be constructed as aconst
object, not that it must be. Therefore there is no requirement that a derived class must also beconst
-constructible.