Inheritance between contracts is indicated by the is keyword in the contract declaration. Update ContractA so that it isContractB, and delete the whoAmI function from ContractA.
Copy
Ask AI
contract ContractB { function whoAmI() external pure returns (string memory) { return "contract B"; }}contract ContractA is ContractB {}
Deploy and test again. Even though ContractA doesn’t have any functions in it, the deployment still shows the button to call whoAmI. Call it. ContractA now reports that it is “contract B”, due to the inheritance of the function from Contract B.
Contracts can call the internal functions from contracts they inherit from. Add an internal function to ContractB called whoAmIInternal that returns “contract B”.
Add an external function called whoAmIExternal that returns the results of a call to whoAmIInternal.
Copy
Ask AI
contract ContractB { function whoAmI() external pure returns (string memory) { return "contract B"; } function whoAmIInternal() internal pure returns (string memory) { return "contract B"; }}contract ContractA is ContractB { function whoAmExternal() external pure returns (string memory) { return whoAmIInternal(); }}
Deploy and test. Note that in the deployment for ContractB, the whoAmIInternal function is not available, as it is internal. However, calling whoAmIExternal can call the internal function and return the expected result of “contract B”.
You cannot call a private function from a contract that inherits from the contract containing that function.
Copy
Ask AI
// Bad code example, do not usecontract ContractB { function whoAmIPrivate() private pure returns (string memory) { return "contract B"; }}contract ContractA is ContractB { function whoAmExternal() external pure returns (string memory) { return whoAmIPrivate(); }}
A contract that inherits from another contract will have that contract’s bytecode included within its own. You can view this by opening settings in Remix and turning Artifact Generation back on. The bytecode for each compiled contract will be present in the JSON file matching that contract’s name within the artifacts folder.
contract notEmptyContract { function sayHello() public pure returns (string memory) { return "To whom it may concern, I write you after a long period of silence to alert you that after much reflection, it occurs to me that I don't think you have fully considered..."; }}
Will have more complex bytecode. In this case, mostly to store the long string present in the return:
In this lesson, you’ve learned how to use inheritance to include the functionality of one contract in another. You’ve also learned that inheriting contracts can call internal functions, but they cannot call private functions. You’ve also learned that inheriting from a contract adds the size of that contract’s bytecode to the total deployed size.