0
0
Blockchain / Solidityprogramming~5 mins

Contract inheritance in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Contract inheritance lets you create new contracts based on existing ones. This helps you reuse code and add new features easily.

You want to build a new contract that shares common features with another contract.
You need to add extra functions to a contract without changing the original code.
You want to organize your contracts better by separating common logic into a base contract.
You want to upgrade or extend a contract's behavior safely.
You want to avoid repeating the same code in multiple contracts.
Syntax
Blockchain / Solidity
contract ChildContract is ParentContract {
    // new or overridden code here
}
Use the keyword is to inherit from one or more parent contracts.
You can override functions from the parent contract by redefining them in the child contract.
Examples
The Child contract inherits the greet function from Parent.
Blockchain / Solidity
contract Parent {
    function greet() public pure returns (string memory) {
        return "Hello from Parent";
    }
}

contract Child is Parent {
    // inherits greet() from Parent
}
The Child contract overrides the greet function to change its behavior.
Blockchain / Solidity
contract Parent {
    function greet() public pure virtual returns (string memory) {
        return "Hello from Parent";
    }
}

contract Child is Parent {
    function greet() public pure override returns (string memory) {
        return "Hello from Child";
    }
}
Contract C inherits from two contracts A and B, gaining both functions.
Blockchain / Solidity
contract A {
    function foo() public pure returns (string memory) {
        return "A";
    }
}

contract B {
    function bar() public pure returns (string memory) {
        return "B";
    }
}

contract C is A, B {
    // inherits foo() from A and bar() from B
}
Sample Program

This program shows a base contract Animal with a function sound. Two contracts Dog and Cat inherit from Animal and override sound to return their own sounds. The Test contract creates instances and calls their sounds.

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Animal {
    function sound() public pure virtual returns (string memory) {
        return "Some sound";
    }
}

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

contract Cat is Animal {
    function sound() public pure override returns (string memory) {
        return "Meow";
    }
}

contract Test {
    function testSounds() public pure returns (string memory, string memory) {
        Dog dog = new Dog();
        Cat cat = new Cat();
        return (dog.sound(), cat.sound());
    }
}
OutputSuccess
Important Notes

Remember to mark functions in the parent contract as virtual if you want to allow overriding.

Use override keyword in the child contract when you change a function from the parent.

Multiple inheritance is allowed, but be careful with function name conflicts.

Summary

Contract inheritance helps reuse and extend code in blockchain contracts.

Use is keyword to inherit from one or more contracts.

Override functions with virtual and override keywords to customize behavior.