OOPs Concepts

Inheritance

Beginner Level

Inheritance in Solidity allows one contract to derive properties and functions from another contract, enabling code reuse and modular design. This is similar to inheritance in object-oriented programming languages like C++ or Java.

By using inheritance, you can avoid rewriting code and create contracts that extend the functionality of existing contracts. Solidity supports single, multiple, and multilevel inheritance.

Key Points About Inheritance

  • Base Contract: The contract whose properties and functions are inherited.
  • Derived Contract: The contract that inherits properties and functions from the base contract.
  • is Keyword: Used to indicate inheritance.
  • Function Overriding: Derived contracts can override base contract functions using the override keyword.
  • Constructor Inheritance: Base contract constructors can be called from derived contracts.

1. Single Inheritance

Single inheritance occurs when a contract inherits from only one base contract.


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

// Base contract
contract Vehicle {
    string public brand;

    function setBrand(string memory _brand) public {
        brand = _brand;
    }
}

// Derived contract
contract Car is Vehicle {
    uint256 public speed;

    function setSpeed(uint256 _speed) public {
        speed = _speed;
    }
}

Explanation:

  • Car inherits from Vehicle.
  • Car can access setBrand() and brand directly without redefining them.
  • This allows modular code reuse.

2. Multiple Inheritance

Multiple inheritance occurs when a contract inherits from more than one base contract. Solidity resolves conflicts using C3 Linearization (method resolution order).


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Engine {
    function startEngine() public pure returns (string memory) {
        return "Engine started";
    }
}

contract Wheels {
    function rotateWheels() public pure returns (string memory) {
        return "Wheels rotating";
    }
}

// Derived contract inherits from both Engine and Wheels
contract Bike is Engine, Wheels {}

Explanation:

  • Bike inherits functions from both Engine and Wheels.
  • Bike can directly call startEngine() and rotateWheels().

3. Multilevel Inheritance

Multilevel inheritance occurs when a contract inherits from a derived contract, forming a chain of inheritance.


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract LivingBeing {
    function breathe() public pure returns (string memory) {
        return "Breathing...";
    }
}

contract Animal is LivingBeing {
    function makeSound() public pure returns (string memory) {
        return "Animal sound";
    }
}

contract Dog is Animal {
    function bark() public pure returns (string memory) {
        return "Bark!";
    }
}

Explanation:

  • Dog inherits from Animal, which inherits from LivingBeing.
  • Dog can access breathe(), makeSound(), and bark().
  • This forms a hierarchical chain of inheritance.

Summary

  • Single inheritance: One base contract → one derived contract.
  • Multiple inheritance: One derived contract → multiple base contracts.
  • Multilevel inheritance: A chain of contracts where each derived contract inherits from the one above it.

Inheritance in Solidity is a powerful feature that allows contracts to reuse code, extend functionality, and create modular, well-organized smart contracts.