What is Inheritance in Solidity?
Let's take a closer inspection at Inheritance in Solidity. Inheritance is an advanced concept in #solidity and also in other languages, too. Inheritance is a way to extend the functionality of a contract - for example when you have something that belongs to you and then you want to give access to somebody else. Solidity supports both single as well as multiple inheritance, so you can inherit once or you can have it multiple.
The contract in Solidity is similar to a class in C++. In C++ you can inherit things. So you can inherit traits from a class and then in a contract. It further demonstrates the apparent similarities between C++ and Solidity. Classes can inherit and why not contracts, too?
How do we inherit? Let's take two contracts to explain this.
Now, if we want the SecondContract to inherit the information from the FirstContract we will use the keyword 'is'. Just like that, the second contract inherits the first contract.
Next, we will test it by taking an exercise and go through the solution together.
Exercise:
Create two contracts A and B
Begin with writing two contracts A and B. Contract A has a state variable that should be initialized to 100.
领英推荐
Also, in the first contract we have a fully accessible function (public) which returns 10 to any given input.
Next point Contract B should inherit from contract A.
First function is called outerAddTen which returns the calculation from innerAddTen function in Contract A to any given input.
And second function which returns the value of innerVal from contract A.
That is it. Let's deploy both contracts and look if everything is working.
See you in the next articles about #solidity , #cryptocurrencies , #smartcontract , #evm and #web3development .
I uploaded this code to my GitHub account.