-
A base contract is an inherited contract also known as a parent contract.
-
The functions are Single Inheritance, Multi-Level Inheritance, Hierarchical Inheritance and, Multiple Inheritance.
-
The same way a Single inheritance helps in inheriting the variables, functions, modifiers, and events of base contracts into the derived class, here a hierarchical inheritance functions as a base contract for multiple derived contracts.
- The Base contract is the main or parent contract from where child contracts inherit
- All public and internal scoped functions are available for derived contracts
- Hierarchical inheritance is when several child contracts derive from the same parent contract
Hi @Maia,
Q1 & Q3
Q2 Which functions are available for derived contracts?
No… these are all different types of inheritance structure. They refer to how the relationship between different base contracts and derived contracts is organised.
This question refers to the different types of visibility that functions can have: public, private, internal, external. Visibility determines where a function (or state variable) can be accessed from. As inheritance requires a derived contract to have access to certain functionality within a base contract, visibility determines the extent of this access.
Functions in the base/parent contract with public or internal visibility are inherited by the derived contract (i.e. they are available), but those marked external or private are not inherited.
Q 3 What is hierarchical inheritance
More or less… hierarchical inheritance is where a single base contract is inherited by more than one (i.e. multiple) derived contracts.
Single inheritance is where a single base contract is inherited by a single derived contract.
Let me know if anything is unclear, or if you have any questions
Nice answers @yestome
Just one comment…
Yes, it’s the parent contract, but I wouldn’t say “main” contract, because it’s not clear what this means. The derived contract is the one that is deployed, so maybe that could be considered the “main” contract. Also, it’s possible to have more than one base contract in an inheritance structure, and so then the question becomes: which one is the main contract?
Let me know if you have any questions
-
A base contract is the parent contract being inherited in another contract.
-
All public and scoped functions of the parent contract are available to derived contracts.
-
Hierarchical inheritance is when a single parent contract is derived into multiple child contracts.
Nice answers @Randallcrum
… but I think you’ve missed out an important word in your answer to Q2…
All public and ??? scoped functions
Ah! internal scoped functions.
- The base contract is the parent contract of the all child contract that inherit from him.
- All public and internal scoped functions and state variables are available to derived contracts.
- Is when a single contract acts as a base contract for multiple derived contracts.
Please, I need same clarification here:
You said " Functions in the base/parent contract with public or internal visibility are inherited by the derived contract (i.e. they are available), but those marked external or private are not inherited".
Did you mean “Public or External” and “Internal or Private” instead? I’m confused.
In other words, is it correct to say that a base/parent contract with “Public or External” visibility can have its functions inherited by the derived contract. However, if the base/parent contract has “Private or Internal” visibility, the derived contract cannot inherit the functions?
I was thinking the Internal or Private visibility be sort of “protected” but still be inherited by the derived contract. Please, correct me if I’m wrong.
Thank you Jon,
Maia
Hi @Maia,
Yes… and that is correct.
No
No… that’s incorrect.
A function with public visibility is available to be called from anywhere, and so would obviously be inherited by a derived contract.
A function with private visibility is only available to be called from another function within the same contract, and so is not inherited by a derived contract.
A function with internal visibility is available to be called from another function within the same contract or its derived contracts (and so is inherited by a derived contract). Internal visibility is like private visibility but extended to derived contracts of the contract the internal function is within.
A function with external visibility is only available to be called from outside of the same contract including its derived contracts, and so is not inherited by a derived contract. This means it is also available to be called from an external service (e.g. Remix or the frontend of a dapp). External visibility is the exact opposite of internal visibility: an external function can be called from anywhere where an internal function can’t be called from, and vice versa
I hope that’s clearer now, but just let me know if you have any further questions
1.What is the base contract?
1.The contract that is inherited is called the parent contract. the parent contract is known as the base contract
2.Which functions are available for derived contracts?
2.All public and internal scoped functions and state variables are available to derived functions.
3. What is hierarchical inheritance?
3. A single contract acts as a base contract(parent) for multiple derived contracts. ex: contract a{} contract b is a {}, contract c is a {},
- the parent contract.
- Polymorphism
- It is similar to simple inheritance
Wow, memorized for life now. Thank you very much Jon!
Hi @cryptoforyou,
Q1
Q2 Which functions are available for derived contracts?
No … … functions with public and internal visibility in the parent contract are inherited by the derived contract (i.e. available). Functions with private and external visibility aren’t inherited.
Q3 What is hierarchical inheritance
… but how is it different?
Single inheritance is where a single base contract is inherited by a single derived contract.
Hierarchical inheritance is where a single base contract (this is the similarity) is inherited by more than one (i.e. multiple) derived contracts (this is the difference).
Let us know if anything is unclear, or if you have any questions
- What is the base contract?
The base contract is the parent contract from which other contracts are derived.
- Which functions are available for derived contracts?
A derived contract or a child contract can use all the functions that are available on the base contract.
- What is hierarchical inheritance?
Hierarchical inheritance refers to the existing hierarchy between contracts with a parent-child relationship, even when multiple contracts are derived from the same parent.
-
What is the base contract?
The parent contract. -
Which functions are available for derived contracts?
All state variables, as well as public and internal functions. -
What is hierarchical inheritance?
It occurs when a parent contract has multiple child contracts.
- What is the base contract?
Known as parent contract, base contract are contracts from which other contracts inherit. - 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?
This is when a single contract acts as a base contract for multiple derived contracts.
thanks for flagging this lack of precision which is perfectly understandable! I’ll keep it in mind especially in the case of several base contracts.
Hi Ernest,
Q1 & Q3
No… not all functions…those with public or internal visibility are available for derived contracts. But functions with private or external visibility are not inherited.
The key feature of hierarchical inheritance is that multiple contracts are derived from the same parent.
Hi @Victrix,
Q1 & Q3
State variables also need to have public or internal visibility to be inherited. State variables marked private are not available for derived contracts. On the other hand, all modifiers and all events in the parent contract are inheried.
Let me know if you have any questions