What is the base contract?
The parent contract that other (derived) contracts inherit from.
Which functions are available for derived contracts?
Public and internal functions.
What is hierarchical inheritance?
When a single base contract serves as the parent for multiple derived classes. In other words, multiple contracts inheriting from the same parent.
There is actually an error in the article where it mentions the derived class. Contracts in Solidity operate in a similar way to classes in other object-oriented programming languages, and so the correct alternative terms are as follows:
parent contract = base contract = base class
child contract = derived contract = derived class
Q2 Which functions are available for derived contracts?
It is true that all functions, except those with private visibility, in the base contract are inherited by derived contracts (i.e. public, internal and external functions). However, unlike public and internal functions, external functions in the base contract are not available to be called from within the derived contract itself. When the derived contract is deployed, external functions in the base contract will only be available to call externally e.g. from Remix, the front-end interface of a dapp, or an external (i.e. non-derived) contract.
Likewise, unlike public, private and internal functions, any external functions in the derived contract are also not available to be called from within the derived contract itself.
Just let me know if anything is unclear, or if you have any questions.
Q2 Which functions are available for derived contracts?
This question is asking which functions in the base contract are inherited and available to be called from within the derived contract.
All functions, except those with private visibility, in the base contract are inherited by derived contracts (i.e. public , internal and external functions).
However, only public and internal functions in the base contract are available to be called from within the derived contract itself — this is the answer.
When the derived contract is deployed, any external functions (inherited from the base contract and/or in the derived contract itself) will only be available to call externally e.g. from Remix, the front-end interface of a dapp, or an external (i.e. non-derived) contract.
Let me know if anything is unclear, or if you have any questions.
Hierarchical inheritance is again similar to simple inheritance. For instance, a single contract acts as a base contract for multiple derived contracts.
But I wouldn’t call the parent/base contract the main contract, as I think that could be misleading. The main purpose of a parent/base contract is to be inherited, and therefore to “supply” the derived contract with other, often more specialised, supporting functionality. When a derived contract is compiled, the functionality it inherits from its parent(s) is incorporated into a single set of bytecode, which is then deployed as a single smart contract at a single Ethereum address on the Ethereum blockchain.
Also, it’s important to understand that a base contract is any parent contract that is inherited by a derived contract. Within an inheritance structure there can be more than one base contract e.g.
// Multi-level inheritance structure
contract A { ... }
contract B is A { ... }
contract C is B { ... }
In this multi-level inheritance structure:
C is a derived contract
A is a base contract… but not the only base contract …
B is both a derived contract (of A), and a base contract (to C)
Basically…
parent contract = base contract
child contract = derived contract
They are just alternative terms for the same thing.
Q2
No… not all of the functions in a parent contract are available for its derived contract(s). Functions with public or internal visibility are available, but not those with private visibility.
A function in the parent contract with external visibility will be inherited by a derived contract, but only to the extent that, when the derived contract is deployed, this function will be available to call externally e.g. from Remix, a web3 client (such as the front-end interface of a dapp), or from an external (i.e. non-derived) contract. However, unlike public and internal functions, an external function in the parent contract is not available to be called from within the derived contract itself.
The only functions which aren’t inherited are those with private visibility.
Let me know if anything is unclear, or if you have any questions
… it’s singlenotsimple inheritance. There is an error in the article, here. This type of inheritance is correctly termed single inheritance in all the other places it’s mentioned in the article.
Functions in the base contract with public (notprivate) visibility and internal visibility are available for the derived contract(s).
Functions with private visibility are not inherited, and only available to be called from within the same contract they are actually defined in.
A function in the base contract with external visibility is inherited by a derived contract, but only to the extent that, when the derived contract is deployed, this function will be available to call externally e.g. from Remix, a web3 client (such as the front-end interface of a dapp), or from an external (i.e. non-derived) contract. However, unlike public and internal functions, an external function in the base contract is not available to be called from within the derived contract itself.
Q3
This part of your answer is correct … and to be more specific …
Hierarchical inheritance is where more than one (multiple) child contracts are derived from the same, single parent contract.
But your example, which partly uses Solidity syntax, is confusing …
The is keyword declares which parent contract(s) is/are inherited by the derived contract being defined i.e.
// Single inheritance
contract Child is Parent { ... }
// Multiple inheritance
// Same, single child is derived from more than one (multiple) parent contracts
contract Child is Parent_A, Parent_B, Parent_C { ... }
// Hierarchical inheritance
// Same, single parent is inherited by more than one (multiple) child contracts
contract Child_A is Parent { ... }
contract Child_B is Parent { ... }
contract Child_C is Parent { ... }
So, your example of hierarchical inheritance would be clearer and more accurate, as follows …
contract B is A { ... }
contract C is A { ... }
contract D is A { ... }
By the way, don’t forget to also post your solutions to the 3 coding assignments which come a bit earlier in the course. This will give you the opportunity to get some useful feedback about your progress writing actual Solidity code…
Events Assignment
This is the assignment from the Events video lecture, which is near the end of the Additional Solidity Concepts section of the course.
Transfer Assignment
This is the assignment from the Payable Functions section of the course, where you have the task of completing the withdraw() function.
Data Location Assignment
This assignment comes just before the Events Assignment, near the beginning of the Additional Solidity Concepts section of the course.
Just let me know if anything is unclear, or if you have any questions about any of these points.
1. What is the base contract?
It’s the parent contract, the contract that is derived, that is inherited. 2. Which functions are available for derived contracts?
The public and internal ones. External functions can be derived too but not accessed by the derived contract. 3. What is hierarchical inheritance?
It’s when a child contract (derived contract) is used as a parent contract by another derived contract.