Hierarchical Inheritance
Hierarchical Inheritance is another type of inheritance in Solidity that allows a child contract to inherit from multiple parent contracts. In this type of inheritance, there is a single parent contract that acts as a base contract and the child contract inherits from it, while other parent contracts inherit from this base contract.
Example
In this example, the Animal
contract acts as the base contract, while the Cat
and Dog
contracts inherit from it. The CatDog
contract then inherits from both the Cat
and Dog
contracts, allowing it to access functions and variables from both parent contracts. The speak
function is overridden in the CatDog
contract to return the combined output of both the Cat
and Dog
speak
functions.
Last updated