🔮
Ethereum
  • General
    • What is Ethereum
      • Ethereum & Bitcoin General Comparison
      • Singleton State
      • The Ethereum Virtual Machine (EVM)
      • Opcodes (operation codes) EVM
      • Ethereum Client
      • Forks
      • Gas (wei)
        • EIP-1559
        • Table Conversion (wei)
      • Proof of Stake (PoS)
      • Proof of Authority (PoA)
      • The Beacon Chain
      • Networks
        • Ethereum mainnet
        • Goerli
      • Account-based model & UTXO-based model
      • Externally Owned Account (EOA)
      • Node Clients
        • Geth
        • Nethermind
      • Contract Account
      • Smart Contract Upgradeability
      • Ultrasound Money
      • Merkle Trees
        • Patricia Merkle Tree
      • Tries
        • State Trie
        • Storage Trie
        • Transactions Trie
        • Receipts Trie
      • Transactions
        • Ethereum Transaction Architecture
      • World State
        • Chain of States
        • Chain of Blocks
        • Stack of Transactions / Mempool
      • Contract Creation
      • Message Call Transaction
      • P2P Network
      • Web3.js
      • Ether.js
        • Smart Contract Interaction Example
      • Web3.js vs Ether.js
      • Node Providers
      • ENS (Ethereum Name Service)
      • Web3 dapp
      • Escrow
      • Multi-signature
      • ERC-20 tokens
        • Send ERC20s to Contracts
      • NFTs
        • ERC-721 and ERC-1155
      • Solidity
        • State Variables
        • Data Location
        • Numbers
        • Modifiers
        • View & Pure Modifiers
        • Data Types
          • Modifiers
          • Modifiers (Functions)
          • Address & Address Payable
        • Hardhat
        • Payable Functions
        • Receive Function
        • Fallback Function
        • Global Variables
        • Self Destruct
        • Create2 Function
        • Revert function
        • Require function
        • Assert Function
        • Calldata
        • Interface
        • Mapping
        • Array
        • Struct
        • Inheritance
          • Virtual & Overwrite
          • Multiple inheritance
          • Hierarchical Inheritance
        • Events
          • Indexed (keyword)
          • LOG0 - LOG4
        • Multi-signature Example
        • Smart Contracts
          • Context
      • Application Binary Interface (ABI )
  • Extras
    • Terminology
      • Bytecode
      • Keccak-256
      • Turing complete
Powered by GitBook
On this page
  1. General
  2. What is Ethereum
  3. Solidity
  4. Inheritance

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

pragma solidity ^0.8.0;

// Base contract
contract Animal {
    string public animalType;

    constructor(string memory _animalType) {
        animalType = _animalType;
    }

    function speak() public virtual returns (string memory);
}

// Child contract that inherits from the Animal contract
contract Cat is Animal {
    constructor() Animal("Cat") {}

    function speak() public override returns (string memory) {
        return "Meow";
    }
}

// Another child contract that inherits from the Animal contract
contract Dog is Animal {
    constructor() Animal("Dog") {}

    function speak() public override returns (string memory) {
        return "Woof";
    }
}

// Child contract that inherits from both the Cat and Dog contracts
contract CatDog is Cat, Dog {
    constructor() Cat() Dog() {}

    // Override speak function from Animal contract
    function speak() public override(Cat, Dog) returns (string memory) {
        string memory catSpeak = Cat.speak();
        string memory dogSpeak = Dog.speak();
        return string(abi.encodePacked(catSpeak, " ", dogSpeak));
    }
}

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.

PreviousMultiple inheritanceNextEvents

Last updated 2 years ago