Inheritance Reading Assignment

Hey jon, I appreciate the clarification !!! :smiley:

1 Like
  1. What is the base contract? The parent contract.
  2. Which functions are available for derived contracts? Everything from the base contract is copied in the derived contract
  3. What is hierarchical inheritance?
    contract A{}
    contract B is A{}
    contract C is A{}
1 Like

Hi @vili,

Q1 & Q3 :ok_hand:

No… not everything in the base contract is inherited:

  • Functions with public or internal visibility are available for derived contracts. But functions with private or external visibility are not inherited.

  • Similarly, state variables with public or internal visibility are inherited, but not those marked private. (Note: state variables can’t have external visibility)

  • All modifiers and events are available for derived contracts.

Let me know if anything is unclear, or if you have any questions :slight_smile:

  1. The base contract (aka parent contract) is that contract that provides for inheritance. Derived class contracts (child contracts) inherit from the base contract.
  2. All public and internal functions are available to derived contracts.
  3. Hierarchical inheritance is when a base contract provides for multiple sets of simple inheritance contract flows.
1 Like
  1. Base contract is the parent contract from which the child contract is inherited.
    2.All public and internal functions.
    3.A parent contract being inherited to two different contracts.
1 Like

Nice answers @Kippie :ok_hand:

By the way, the article uses both single and simple inheritance to refer to the same inheritance structure. As far as I am aware, the correct term is single inheritance (and calling it simple inheritance is an error).

Hi @Deini-Atakan,

Your answers are correct, but I just want to clarify a couple of things…

The base contract is inherited by the child contract.
The child contract inherits the base contract
From your answer, I think you do know this, and it’s just a confusion with how the verb inherit works in English…
I hope you don’t mind me pointing it out.

This is correct, but more than two different contracts can inherit from the same parent as well.
Hierarchical inheritance is where more than one (multiple) derived contracts all inherit from the same, single parent contract.

Just let me know if you have any questions :slight_smile:

Hi, @jon_m I still have some questions, I would really appreciate it if you can answer them.

I didn’t quite get this part. Is the base contract first inherited version of the contract?

And for this part I totally forgot to write “or more” after two while explaining but thanks for pointing it out.

1 Like

1-the base contact os the parent contract
2-all public and internal scoped functions are available
3-a single contract acts as a base contract for multiple derived contracts

1 Like

Hi Atakan,

The base contract (or parent contract) is inherited by the derived contract (or child contract). This means that when we deploy the derived contract, the following functionality in the base contract is also made available within the derived contract:

  • public and internal functions
  • public and internal state variables (including arrays and mappings)
  • all events and modifiers

The base contract doesn’t need to be deployed for this functionality to be available within the deployed derived contract.

These are just 2 alternative ways of saying the same thing in English.

Sorry, if I confused you. Does that make sense now?

Hi @jon_m, to be honest, it still is kind of messed up in my head, either English making it seem hard to understand or I don’t get it. I understood how to use it but I still am not sure what to call what. For example, if I have a destroyable.sol contract which has destroyable contract in it. When I call that function inside destroyable contract from another contract, what should I call them? I hope I asked properly.

Hey @jon_m, yeah I missed that bit on Q2, I gotta get used to specifying the visibility when describing functions. And regarding the Q3 I thought they meant that multiple contracts derived from the same parent is one feature of hierarchical inheritance, didn’t it was the main feature. Thanks for your help :call_me_hand:

1 Like

Hi Atakan,

Don’t worry, that’s quite normal with these kinds of concepts… it can take a while to work out what’s really going on :wink:
I think what may be confusing you is that in our assignment example we are not deploying the Ownable or the Destroyable contracts. When we deploy the derived contract Bank, because Bank inherits from Destroyable, the destroy() function in Destroyable is available within the deployed Bank contract. Or put another way, Bank inherits the destroy() function from Destroyable.

So, when the destroy() function is called (in our example, by the contract owner’s external wallet address), it is being called on the deployed Bank contract (where it’s available by inheritance from Destroyable). The Bank contract doesn’t call the function on Destroyable, because it’s already available in Bank by inheritance — and available to be called externally because it has public visibility.

Bank is the derived (or child) contract which inherits from Destroyable (the base contract, or parent contract)
or to say it another way in English…
Destroyable is the base (or parent contract) which is inherited by Bank (the derived, or child contract).

The flow of inheritance goes from Ownable => Destroyable => Bank
This is a multi-level inheritance structure (3 levels) …

contract Ownable {...}

contract Destroyable is Ownable {...}
// Destroyable (Derived/Child) inherits from Ownable (Base/Parent)

contract Bank is Destroyable {...}
// Bank (Derived/Child) inherits from Destroyable (Base/Parent)

Notice that in this multi-level inheritance structure, Destroyable is both a child of Ownable and a parent of Bank.

Any clearer? :slight_smile:

1 Like

It definitely is clearer now but I still probably won’t be able to explain it to someone yet. I get what they mean but explaining is a whole another level :smiley:. Thank you so much @jon_m

1 Like

Well that’s good progress :slight_smile:

One step at a time, Atakan … you’ll get there. Just know that some more complicated concepts do take a while before they truely click and you are able to explain them clearly to others. Keep persevering … you’re doing great :muscle:

1 Like

Hello

Here are my answers for the assignment.

  1. A base contract is the parent contract in the inheritance tree.
  2. All of the functions from the base or parent contract.
  3. The derived contracts inherit directly from the base contract.

Hi @darthgawd

Q1 :ok_hand:

Q2  Which functions are available for derived contracts?

No… functions with public or internal visibility are inherited, but those with private or external visibility are not.


Q3  What is hierarchical inheritance?

While this is true, the key feature of hierarchical inheritance is that more than one (multiple) derived contracts inherit from the same, single base contract.

Let me know if anything is unclear, or if you have any questions :slight_smile:

Jon. I got it now, thank you for the response.

1 Like
  1. The base contract refers to the parent contract in the line of inheritance.
  2. Functions that are available for derived contracts include functions that are public and are internal within the base contract.
  3. Hierarchical structure allows for one or more child contracts to inherit from the same parent contract.
1 Like

Hi @loso,

Q1 & Q2 :ok_hand:

Q3

Nearly… hierarchical inheritance is where more than one (i.e. multiple) child contracts inherit from the same, single parent contract.
Where there is only one child contract which inherits from a single parent contract, this is known as single inheritance.

Let me know if you have any questions :slight_smile: