Inheritance Reading Assignment

Good answers @JonPag :ok_hand:

Yes … hierarchical inheritance is where more than one child contract inherits from the same, single parent contract.

Just to confirm … this isn’t hierarchical inheritance (which is what I think you mean). If you mean …

contract Base { ... }
contract A is Base { ... }
contract B is A { ... }

… then this is an example of multi-level inheritance. An example of hierarchical inheritance would be …

contract Base { ... }
contract A is Base { ... }
contract B is Base { ... }

Nice answers @santiago :ok_hand:

… good to see you back here in the forum :slight_smile:

1 Like
  1. What is the base contract?
    The parent contract that other (derived) contracts inherit from.

  2. Which functions are available for derived contracts?
    Public and internal functions.

  3. 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.

1 Like
  1. What is the base contract? The parent contract and the derived class.

  2. Which functions are available for derived contracts? all publi and internal scoped functions.

  3. What is hierarchical inheritance? is when the base contract is the base contract for miltiple derived contracts.

1 Like

Nice answers @Scott815 :ok_hand:

Just to clarify …

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

So your answer should be …

The parent contract and the base class.

Let me know if you have any questions.

  1. The base contract is the contract which other contracts inherit from.
  2. The functions that are not private from the base function and whatever functions the contract itself has.
  3. Hierarchical inheritance refers to more than one contract that inherit from the same contract.
1 Like
  1. Parent contract
    2.Single inheritance helps in inheriting the variables, functions, modifiers, and events of base contracts into the derived class.
  2. Hierarchical inheritance is again similar to simple inheritance. however, a single contract acts as a base contract for multiple derived contracts.
1 Like

Nice answers @Xabier22 :ok_hand:

Just one comment …

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.

Hi @OzzieAl,

Q1 & Q3 :ok_hand:

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.

  1. The parent contract is known as a base contract.
  2. Public and internal scoped functions.
  3. Hierarchical inheritance is again similar to simple inheritance. For instance, a single contract acts as a base contract for multiple derived contracts.
1 Like
  1. the class that is inherited by the child classes

  2. public and internal functions

  3. the inheritance that base class has multiple derived classes

1 Like
  1. What is the base contract?
    Is the main contract, the parent contract where all child contracts derive from.

  2. Which functions are available for derived contracts?
    All functions from the main parent is available in the derived contract.

  3. What is hierarchical inheritance?
    Where a single parent contract acts as a base/parent for multiple derived contracts.

1 Like

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