Blog

Constructor Inheritance & Override Keyword

If the Parent contract has an initial value set in a constructor, the Child contract will inherit this constructor.

When deploying the Child contract, it will call the Parent contract's constructor, initializing any state variables as defined in the Parent constructor. Here’s how you can implement and demonstrate this:




Virtual & Override

~When a function in a parent contract is declared as virtual, it indicates that the function can be overridden by a function with the same name and signature in a child contract.

~The virtual keyword allows child contracts to provide their own implementation of the function, effectively overriding the behavior defined in the parent contract.

~Functions marked as virtual can have default implementations in the parent contract, but they are intended to be customized by child contracts.

~The virtual keyword is placed in the function declaration in the parent contract.


Override

~When a function in a child contract is declared as an override, it indicates that the function is overriding a virtual function from a parent contract.

~The override keyword ensures that the function in the child contract has the same name, parameter types, and return type as the function it is overriding in the parent contract.

~It serves as a safety mechanism to prevent accidental function signature mismatches between parent and child contracts.