#100DaysOfSolidity 📚 The Power of Inheritance in Solidity 🏛️ #23

#100DaysOfSolidity Series 023 “Inheritance”

Solidity Academy
4 min readJul 3, 2023

Solidity, the programming language for Ethereum smart contracts, offers powerful features for code reusability and modularity. One such feature is inheritance, which allows contracts to inherit properties and functions from other contracts. Inheritance enables developers to create hierarchies of contracts, promoting code reuse, reducing redundancy, and enhancing the maintainability of smart contracts.

#100DaysOfSolidity Series 023 “Inheritance”

In this article, we will explore the concept of inheritance in Solidity, understand how it works, and examine some practical examples.

Understanding Inheritance

Inheritance in Solidity follows a similar concept as inheritance in object-oriented programming languages. Contracts can inherit from one or more parent contracts, thereby acquiring their state variables and functions. The keyword “is” is used to specify inheritance relationships between contracts. Let’s dive into the details of how inheritance works in Solidity.

Syntax and Order of Inheritance

The syntax for inheritance in Solidity is straightforward. Contracts specify their inheritance by using the “is” keyword, followed by the name(s) of the parent contract(s). The order of inheritance is crucial and should be listed from the “most base-like” contract to the “most derived” contract.

Consider the following contract hierarchy:

/* Graph of inheritance
A
/ \
B C
/ \ /
F D,E
*/
contract A {
function foo() public pure virtual returns (string memory) {
return "A";
}
}
contract B is A {
function foo() public pure virtual override returns (string memory) {
return "B";
}
}
contract C is A {
function foo() public pure virtual override returns (string memory) {
return "C";
}
}
contract D is B, C {
function foo() public pure override(B, C) returns (string memory) {
return super.foo();
}
}
contract E is C, B {
function foo() public pure override(C, B) returns (string memory) {
return super.foo();
}
}
contract F is A, B {
function foo() public pure override(A, B) returns (string memory) {
return super.foo();
}
}

In the above example, contract A is the base contract, while contracts B and C inherit from A. Contract D inherits from both B and C, and contract E inherits from C and B. Finally, contract F inherits from A and B.

Order of inheritance is important because it determines the order in which function implementations are searched when a function is called. The search starts from the rightmost parent contract and proceeds in a depth-first manner.

Function Overriding and Virtual Functions

Inheritance allows child contracts to override functions inherited from parent contracts. To enable function overriding, the parent function must be declared as virtual, and the child function must use the “override” keyword. This mechanism ensures that the child contract provides its implementation of the function.

In the example above, contracts B and C both override the foo() function inherited from contract A. The “override” keyword explicitly indicates that these functions are intended to override their parent implementations.

Additionally, the “virtual” keyword in contract A’s foo() function signifies that it is designed to be overridden by child contracts. It allows the function to be defined in a parent contract and overridden in derived contracts.

Analyzing and Comparing the Smart Contracts

Now, let’s analyze, compare, and comment on the smart contracts provided in the example. We’ll go contract by contract to understand their behavior and the output of the foo() function.

Contract A:

Contract A defines a virtual function foo(), which returns the string “A”. Being the base contract, it serves as the starting point for inheritance in the contract hierarchy.

Contract B:

Contract B inherits from contract A using the “is” keyword. It overrides the foo() function and returns the string “B”. Thus, calling foo() on contract B will result in the output “B”.

Contract C:

Contract C also inherits from contract A and overrides the foo() function. It returns the string “C” when foo() is called on contract C.

Contract D:

Contract D inherits from both contracts B and C. It overrides the foo() function, explicitly specifying the order of the parent contracts using the “override” keyword. In this case, foo() returns the result of calling super.foo(), which resolves to contract C’s implementation. Therefore, calling foo() on contract D will output “C”.

Contract E:

Contract E has a similar inheritance structure as contract D but with the order of parent contracts reversed. When foo() is called on contract E, it returns the output of super.foo(), which refers to contract B’s implementation. Hence, the output will be “B”.

Contract F:

Contract F inherits from contracts A and B. When foo() is called on contract F, it returns the output of super.foo(), which points to contract B’s implementation. Consequently, calling foo() on contract F will produce the output “B”.

Conclusion

Inheritance is a powerful feature of Solidity that allows contracts to reuse code, create contract hierarchies, and enhance code maintainability. Contracts can inherit from multiple parent contracts, and function overriding provides flexibility to customize behavior. By understanding the order of inheritance and properly using the “virtual” and “override” keywords, developers can create robust and modular smart contracts.

Solidity’s inheritance mechanism empowers developers to build scalable and efficient Ethereum smart contracts. By leveraging inheritance, code reuse becomes a reality, reducing redundancy and improving code maintainability. Understanding how inheritance works, the order of inheritance, and the use of virtual and override keywords is crucial for writing efficient and extensible smart contracts.

Now that you have a comprehensive understanding of inheritance in Solidity, you can apply this knowledge to your own projects and create well-structured and reusable code. Solidity’s inheritance feature opens up a world of possibilities for smart contract development. So go ahead, explore, and unlock the full potential of Solidity’s inheritance capabilities!

🔗 Additional Resources:

--

--

Solidity Academy

Your go-to resource for mastering Solidity programming. Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/