[ad_1]
Inheritance is a way to extend functionality of a contract. Solidity supports both single as well as multiple inheritance.
Inheritance is one of the most important features of the object-oriented programming language.it is inspired from Python .
Solidity inheritance lets us combine multiple contracts into a single one. The base contracts are the ones from which other inherit. Those contracts that inherit data are derived. This is a process resulting in parent-child relationships between contracts.There are two types of inheritance: single and multi-level. Inheritance marks several linked contracts with a parent-child relationship.
It rules highly resemble Python, but they have a few differences. Inheritance creates one contract and places it into the blockchain.The inheritance uses the keyword is.
Following are Inheritance features:
- A derived contract can access all non-private members including internal methods and state variables. But using this is not allowed.
- Function overriding is allowed provided function signature remains same. In case of difference of output parameters, compilation will fail.
- We can call a super contract’s function using super keyword or using super contract name.
- In case of multiple inheritance, function call using super gives preference to most derived contract.
Syntax:
pragma solidity ^0.8.10;contract A{
contract B is A {
//statement
}
//statement
}
Example:
contract A {
event Event(string name);constructor() public {
emit Event("A");
}
}
contract B is A {
constructor() public {
emit Event("B");
}
}
contract C is A {
constructor() public {
emit Event("C");
}
}
contract D is C, B {
constructor() public {
emit Event("D");
}
}
we have 4 types of Inheritance in solidity. Following are Inheritance types:
Single Inheritance
In Single or single level inheritance the functions and variables of one base contract are inherited to only one derived contract.
Multi-level Inheritance
It is very similar to single inheritance, but the difference is that it has levels of the relationship between the parent and the child. The child contract derived from a parent also acts as a parent for the contract which is derived from it.
Hierarchical Inheritance
In Hierarchical inheritance, a parent contract has more than one child contracts. It is mostly used when a common functionality is to be used in different places.
Multiple Inheritance
In Multiple Inheritance, a single contract can be inherited from many contracts. A parent contract can have more than one child while a child contract can have more than one parent.
New to trading? Try crypto trading bots or copy trading
[ad_2]
Source link