Original title: "Introduction to Solidity Minimalist: 13. Inheriting Father and Son"
Original author: 0xAA
I am re-learning solidity recently, to consolidate the details, and also write a "Solidity Minimalist Introduction" for beginners to use (programmers can write another Find tutorials), update 1-3 lectures every week.
All codes and tutorials are open source on github: github.com/AmazingAng/WTFSolidity
In this talk, we introduce inheritance in solidity, including simple inheritance, multiple inheritance, and modification Inheritance of modifiers and constructors.
< /p>
Inheritance is an important part of object-oriented programming, which can significantly reduce repetitive code. If the contract is regarded as an object, solidity is also object-oriented programming and also supports inheritance.
< /p>
virtual: The function in the parent contract, if you want the child contract to rewrite, you need to add the virtual keyword.
override: The child contract overrides the function in the parent contract, and the override key needs to be added.
Let’s first write a simple grandpa contract Yeye, which contains 1 Log event and 3 functions: hip(), pop(), yeye (), the output is "Yeye".

< /p>
Let's define another father's contract Baba, let him inherit the Yeye contract, the syntax is contract Baba is Yeye, very intuitive. In the Baba contract, we rewrite the two functions hip() and pop(), add the override keyword, and change their output to "Baba"; and add a new function baba, the output is also "Baba ".

< /p>
We deploy the contract, we can see that there are 4 functions in the Baba contract, among which hip() and The output of pop() is successfully rewritten to "Baba", while the output of the inherited yeye() is still "Yeye".
Solidity contracts can inherit multiple contracts. Rules:
1. When inheriting, arrange in order from highest to lowest seniority. For example, if we write an Erzi contract that inherits the Yeye contract and the Baba contract, then it must be written as contract Erzi is Yeye, Baba instead of contract Erzi is Baba, Yeye, otherwise an error will be reported.
2. If a certain function exists in multiple inherited contracts, such as in the example The hip() and pop() must be rewritten in the sub-contract, otherwise an error will be reported.
3. When rewriting a function with the same name in multiple parent contracts, after the override keyword Add the name of the parent contract, eg override(Yeye, Baba).
Example:

We can see that the Erzi contract has rewritten The hip() and pop() functions change the output to "Erzi", and also inherit the two functions yeye() and baba() from the Yeye and Baba contracts respectively.
Modifiers in Solidity can also be inherited, the usage is similar to function inheritance, just add virtual and override keywords in the corresponding places.
There are two ways for a child contract to inherit the constructor of the parent contract. To give a simple example, there is a state variable a in the parent contract A, which is determined by the parameters of the constructor:
< /p>
1. When inheriting, declare the parameters of the parent constructor, for example: contract B is A( 1)
2. Declare the parameters of the constructor in the constructor of the sub-contract, for example:

There are two ways for a child contract to call a function of the parent contract, directly calling and using the super keyword.
- Direct call: the child contract can directly use the parent contract name. function name () method to call the parent contract function, such as Yeye.pop().

p>
- super keyword: sub-contracts can use super. function name () to call the nearest parent contract function. The solidity inheritance relationship is declared from right to left: contract Erzi is Yeye, Baba, then Baba is the nearest parent contract, super.pop() will call Baba.pop() instead of Yeye.pop():  ;

< /p>
p>
In this lecture, we introduced the inheritance of solidity Basic usage, including simple inheritance, multiple inheritance, inheritance of decorators and constructors, and calling functions in parent contracts.
Original link
Welcome to join the official BlockBeats community:
Telegram Subscription Group: https://t.me/theblockbeats
Telegram Discussion Group: https://t.me/BlockBeats_App
Official Twitter Account: https://twitter.com/BlockBeatsAsia
