Inheritance Reading Assignment

Hi @karendj,

Q3 :ok_hand:


Q1

This part of your answer is correct :ok_hand:

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 :slight_smile:

1 Like
  1. The parent contract
  2. All public and internal scoped functions are available in a derived contract.
  3. Hierarchical inheritance is similar to simple inheritance. Here, however, a single contract acts as a base contract for multiple derived contracts.
1 Like

Nice answers @limitlessandrew :ok_hand:

Just to clarify …

… it’s single not simple 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.

Hi @jon_m thank you for the correction and breaking the answers down, I understand better now.

1 Like
  1. What is the base contract? parent contracts
  2. Which functions are available for derived contracts? private and internal functions
  3. What is hierarchical inheritance? a parent contract where other child contracts are derived from. contract B is A, C and D are B… etc.

Hi @Jason_Purcell,

Q1 :ok_hand:


Q2

Functions in the base contract with public (not private) 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.

Okay I will go over prior assignments and this one again. Thank you for your feedback! I appreciate it and your time. :+1:

1 Like

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.

1 Like

Hi @seybastiens,

Q1

That’s correct :white_check_mark:

But this part isn’t … "“the contract that is derived” is the derived contract (child contract).
But, I can see from your answer to Q3 that you already know this, so I think this may just be a slight confusion over how the verb “derive” and the adjective “derived” are used in English.

Q2

Correct :white_check_mark: … although I think it’s clearer if we say, “External functions can be inherited too” … instead of derived.

Q3

I think that what you’ve described here is multi-level inheritance …

contract A { ... }

contract B is A { ... }
/*  B is a child/derived contract of A (...derived/inherited from A)
    AND   
    B is also a parent/base contract to C
*/ 
contract C is B { ... }

Hierarchical inheritance is where more than one (multiple) child contracts are derived from the same, single parent contract.

contract A { ... }
contract B is A { ... } 
contract C is A { ... }
contract D is A { ... }

The opposite of hierarchical inheritance is multiple inheritance, which is where more than one (multiple) parent contracts are inherited by the same, single child contract.

contract A { ... }
contract B { ... }
contract C { ... }
contract D is A, B, C { ... }

Let me know if you have any questions :slight_smile:

1 Like

Yes, it was a confusion for Q1. By “the contract that is derived” I meant “the contract that we apply a derivation on”, but it’s more confusing than anything in this case.

Ok I confused multi-level and hierarchical inheritance, thanks.

1 Like

I thought that’s what you probably meant. Don’t worry, I still find it a challenge to describe some of these concepts in an English that makes sense … and English is my native language! :sweat_smile:

1 Like

1. What is the base contract?
The base contract is the contract that is inherited.

2. Which functions are available for derived contracts?
All public and internal scoped functions are available to derived contracts.

3.What is hierarchical inheritance?
Hierarchical inheritance is when multiple contracts inherit from one and the same base contract.

2 Likes

Nice answers @pappas_kellyn :ok_hand:

Just to clarify …

… when the derived contract is compiled, yes … but only the compiled bytecode of the functionality that is inherited from the parent contract is incorporated (which may not be all of it).

When the derived contract is deployed, its single set of compiled bytecode (including what has been incorporated from the parent contract) is deployed at a single Ethereum address (contract address). This single address isn’t really shared because there is only one contract that has actually been deployed.

But you’ve made a really good attempt to describe what’s happening here, and you clearly have a good understanding of this :muscle:

1 Like
  1. What is the base contract? It is parent contract.
  2. Which functions are available for derived contracts? Public, internal and state variable.
  3. What is hierarchical inheritance? Single contract act as a base for multiply derived contracts. E.g.: contract A is derived into contract B and C.
2 Likes
  1. Parent contract
  2. Public and internal functions
  3. An inheritance where a base contract acts as a parents for multiple parents
2 Likes
  1. Base contract = parent contract or the contract that is inherited.

  2. All public and internal functions are available to derived contracts

  3. Hierachical inheritance = a slingle contract acts as a base contract for multiple derived contracts.

1 Like

Good answers @martina :ok_hand:

Just to clarify …

As you say, functions in the base contract with public and internal visibility are available to be called from within the derived contract(s). This is also the case for state variables declared in the base contract i.e. those with public and internal visibility are available for the derived contract(s), but not those with private visibility.

Yes … hierarchical inheritance is where the same, single base contract is inherited by more than one (multiple) derived contracts … The base contract isn’t doing any multiplying :wink:

Hi @stanmation,

Q1 & Q2 :ok_hand:


Q3 What is hierarchical inheritance?

Hierarchical inheritance is where the same, single base/parent contract is inherited by more than one (multiple) derived/child contracts.

  1. What is the base contract?

Contract which has children contracts.

  1. Which functions are available for derived contracts?

All public and internal functions are available for child contracts.

  1. What is hierarchical inheritance?

When parent contract derived into the multiple child contacts.

1 Like

Nice answers @xenmayer :ok_hand:

Just to clarify …

Yes … and a base contract can have just one, or multiple child contracts.


Yes … when the same, single parent contract is inherited by more than one (multiple) child contracts.

1 Like