Inheritance Reading Assignment

Hi, this is my answers.

1. What is the base contract?

A base contract is a contract that is inherited by other contracts, in other words, its state variables and functions get injected into other contracts. These other contracts are called derived contracts.

Derived contracts treat these state variables and functions like other state variables and functions of their own (except those with modifier private), which means they can call or change state variables and execute functions of the base contract.

2. Which functions are available for derived contracts?

Functions with modifiers such as public, external and internal are available for derived contracts.

I’m not sure if functions and variables with modifier external can be called by derived contracts, because by definition after inheriting from a base contract, derived contracts treats all the things in the base contract as their own so that they are not external contracts anymore. It confuses me!

3. What is hierarchical inheritance?

When a base contract is inherited by two or more contracts, this type of inheritance is called hierarchical inheritance.

1 Like

Hi @Placebo,

Q1 & Q2 :ok_hand:

Q3

This part is correct :ok_hand: and your answer should finish here.

Polymorphisim in Solidity doesn’t have anything to do with different types of inheritance structure (e.g. hierarchical inheritance, multiple inheritance etc.)
And polymorphism has nothing to do interfaces.

Polymorphism, interfaces and abstract contracts in Solidity are more advanced topics, and what they are and involve, and how we use them, will gradually become clearer with further study and with more experience.

Hi @codingluan,

This is a very good effort at going deeper and getting to grips with the detail of what you’ve learnt from the article :muscle:

Your answers are correct except for how external visibility is affected by inheritance…

Only functions with public and internal visibility in the base contract will be available to be called from within the derived contract itself.

So, functions with public or internal visibility are inherited , as well as any functions in the base contract with external visibility. However, unlike public and internal functions, inherited external functions won’t be available to be called from within the derived contract; when the derived contract is deployed, they will only 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.

The only functions which aren’t inherited are those with private visibility.

State variables cannot have external visibility, so the state variables which are inherited, and which can be referenced, set or reassigned from within the derived contract, are those with public or internal visibility.

Hopefully this also clears up your confusion and resolves the uncertainties you mentioned surrounding external visibility. But do let me know if anything is still unclear, or if you have any further questions :slight_smile:

1 Like

Thanks Jonathan! Your response helps a lot, especially some of the concepts below I overlooked or didn’t know about. Really helpful!!! :+1:

1 Like
  1. What is the base contract?
    The parent contract.

  2. Which functions are available for derived contracts?
    ALL public and internal functions from the parent contract are available to the derived contracts.

  3. What is hierarchical inheritance?
    A type of inheritance where a single contract acts as a base contract for multiple derived contracts.


    https://medium.com/coinmonks/solidity-and-object-oriented-programming-oop-191f8deb8316

1 Like
  1. It is the parent contract that is inherieted by the derived contracts.

  2. All public and internal scoped functions and state variables are available.

  3. It a inheritance which a single contract acts as a base for multiple derived contracts.

1 Like

What is the base contract?
The parent contract is base contract.Solidity compiler copies the base contract bytecode into derived contract bytecode. The base contract is the contract that inherited.
Which functions are available for derived contracts?
All public and internal scoped functions and state variables are available to derived contracts.
What is hierarchical inheritance?
A single contract acts as a base contract for multiple derived contracts.
For example: contract A act as parent for child contract B and C.

1 Like
  1. What is the base contract?
    A parent contract, the contract that is inherited.

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

  3. What is hierarchical inheritance?
    A base contract for multiple derived contracts.

1 Like
  1. This is parent contracts from which inherits child contracts.
  2. All base contract functions.
  3. Single contracts acts as a base contract for multiple derived contracts.
1 Like

Hi @c1cag1b3,

Q1 & Q3  :ok_hand:

Q2 Which functions are available for derived contracts

Not all of the functions in the base contract are available. Functions with public or internal visibility are available for derived contracts, but not those with private visibility.

A function in the base 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 base 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 base contract is the parent contract, the contract that is inherited.

2 All public and internal scoped functions

3.A single contract that acts as a base for multiple derived contracts

1 Like

1. What is the base contract?
The parent contract

2. Which functions are available for derived contracts?
Public functions and internal modifiers only from the base contract it inherits features from. NOT those with private visibility.

3. What is hierarchical inheritance?
A single contract serves as the base contract for multiple derived contracts (aka a parent contract has many children)

1 Like
  1. What is the base contract?
    The parent contract off of which child contracts inherit
  2. Which functions are available for derived contracts?
    Any functions marked public or internal
  3. What is hierarchical inheritance?
    Having multiple child contracts inherit off of one base contract
1 Like

Nice answers @InterstellarX :ok_hand:

Just one clarification …

Public and internal functions are available i.e. functions with public or internal visibility. Modifiers are not marked with a visibility keyword, and all modifiers in the base contract are available for its derived contract(s).

And just to complete the picture …

  • As with functions, both public and internal state variables in the base contract are available for its derived contract(s), but not state variables marked private.
  • Any events declared in the base contract are available to be emitted in its derived contract(s).

Just let me know if you have any questions.

1 Like
  1. A contract can inherit the variables, functions, modifiers, and events from another contract. That contract is called the “base” contract.

  2. all public and internal scoped functions and state variables are available to derived contracts.

  3. A Hierarchical inheritance is like an org. chart. You have a “top” contract that a bunch of other contracts inherit from. You can have contract B, C, D etc that all inherit from contract A at the top of the hierarchy.

1 Like

Nice answers @Tomaage :ok_hand:

Just one comment …

Correct … except that it would be an organisation chart with only one “manager” and their two or more “employees”. Hierarchical inheritance only refers to the inheritance relationship between the same, single base contract, and two or more (multiple) derived contracts which inherit from it (i.e. two levels). You could have a larger inheritance structure with several hierarchical inheritance relationships integrated with each other over several levels. You could have …

  • 3 derived contracts (B, C and D) all inheriting from the same, single “top-level” base contract (A). This would form one hierarchical inheritance relationship.
  • C is also the same, single base contract from which 2 derived contracts inherit (E and F). This would form another hierarchical inheritance relationship.
contract A { ... }            // top level
contract B is A { ... }       // middle level
contract C is A { ... }       // middle level
contract D is A { ... }       // middle level
contract E is C { ... }       // bottom level
contract F is C { ... }       // bottom level

This would then start to look more like the organisation chart that you mentioned as an analogy.

Let me know if you have any questions.

Would a variable, function, modifier or events from contract F in this example, be used in contract A even though its not the Base contract? Is contract C considered the base for E and F?

I also did not understand fully this part of the reading

The contract that is inherited is called the parent contract and the contract that inherits is called the child contract .
Similarly, the contract has a parent known as the derived class and the parent contract is known as a base contract

I understand that the parent contract, is the base contract, and the derived class is the “child”. Is this right?
“The contract has a parent known as the derived class” vs "the parent contract is known as a “base contract” is what im not following .

And when its compiled and deployed, its all just one smart contract with contracts inside on the blockchain with one blockchain adress?

1 Like

No … the “flow of inheritance” is in the other direction. So …

  • Public and internal functions, public and internal state variables, all modifiers, all events and any constructors in A will be available in all three of its derived contracts (B, C and D), and in all of their derived contracts too (in our example E and F).
  • Also available in E and F will be the public and internal functions, public and internal state variables, all modifiers, all events and any constructors in C (E and F’s parent/base contract), but not in B or D.
  • Only derived/child contracts can inherit from their own base/parent contract(s), from which they will also inherit any functionality which their base/parent contract(s) in turn inherit from any base/parent contract(s) of their own i.e. “grandparent” contract(s) … so on and so on “up the chain” of inheritance.

Yes … contract C in our example is both a derived contract of A, and the base contract of E and F


I’m not surprised by your confusion over part of the article, because there is an error in it. The fact that it confused you, and you’re questioning it, actually shows that you are really paying attention and thinking things through :muscle:

Yes, that’s correct.

This is where the error is. Contracts in Solidity operate in a similar way to classes in other object-oriented programming languages, and therefore the following are alternative names for the same thing:

parent contract = base contract = base class
child contract = derived contract = derived class

So, this sentence in the article should, instead, read something like …

The child contract is known as a derived contract, or the derived class; and the parent contract is known as a base contract, or the base class.


If you deploy Bank, then it’s just one smart contract called Bank, deployed on the blockchain at a single address. There are no “contracts inside” Bank. When Bank is compiled, all of the functionality that it inherits from Destroyable and Ownable is automatically included, and the result is a single set of bytecode, which is then deployed at a single Ethereum address on the Ethereum blockchain.

You could also deploy Destroyable, although there wouldn’t be much point, as it only really has any purpose when inherited and compiled together with a derived contract which will utilise its specialised contract-destruction functionality. However, if you did, then it would also just be one smart contract called Destroyable, deployed on the blockchain at a single address. It’s compiled bytecode would include all of the functionality that it inherits from Ownable, but nothing from Bank.

And if you were to just deploy Ownable, then the deployed bytecode would have only been compiled from Ownable, and nothing else.

I hope that makes things clearer. Just let me know if you have any further questions :slight_smile:

  1. What is the base contract?

The contract that is inherited is the parent contract

  1. Which functions are available for derived contracts?

.All public and internal functions and state variables are available for derived contracts.

  1. What is hierarchical inheritance?

Hierarchical inheritance is again similar to simple inheritance. Here, however, a single contract acts as a base contract for multiple derived contracts.

1 Like

Hi, my answers below:

1 Like