Inheritance, Abstract Contracts, and Interfaces
Learn to extend the functionality of your smart contracts with code from other smart contracts, abstract contracts, and interfaces.
We'll cover the following...
Inheritance
In object-oriented programming, inheritance is a program’s ability to use another program’s functionality. In Solidity, it allows us to extend our contracts with code that has been proven to be secure and keep them organized and maintainable. The contract providing this code is known as a base contract, whereas the inheriting contract is known as a derived contract.
We declare that a contract inherits from a source using the is keyword as if to say that the derived contract is an instance of the base contract.
Code from multiple base contracts can be inherited by a single derived contract. Inheritance is also transitive—if contract Y inherits from contract X and Z inherits from Y, then contract Z will be able to use the functionality of X as well as Y, as in the example above.
Note: Only
publicandinternalfunctions and state variables of a base contract are available to derived contracts;externalandprivatevisibility modifiers do not allow inheritance.
Lines 2–11: We create base contracts
sumandproduct, respectively. Each contract ...