Skip to content

Instantly share code, notes, and snippets.

@Davidegloh
Last active September 6, 2021 14:08
Show Gist options
  • Save Davidegloh/e2260cae80dd55c09ae6befce3c8c5b1 to your computer and use it in GitHub Desktop.
Save Davidegloh/e2260cae80dd55c09ae6befce3c8c5b1 to your computer and use it in GitHub Desktop.
[OOP - Inheritance - Solidity] #inheritance #oop
/*Inheritance is one of the pillars of object orientation and Solidity supports inheritance between smart contracts. Inheritance is the process of defining multiple contracts that are related to each other through parent-child relationships.
contract that is inherited = parent contract
contract that inherits = child contract
Inheritance is mostly about code-reusability. There is a is-a relationship between base and derived contracts and all public and internal scoped functions and state variables are available to derived contracts.
In fact, Solidity compiler copies the base contract bytecode into derived contract bytecode. The is keyword is used to inherit the base contract in the derived contract. It is one of the most important concepts that should be mastered by every Solidity developer because of the way contracts are versioned and deployed.
Single inheritance
Single inheritance helps in inheriting the variables, functions, modifiers, and events of base contracts into the derived class.
Multi-level inheritance is very similar to single inheritance; however, instead of just a single
parent-child relationship, there are multiple levels of parent-child relationship.
Hierarchical inheritance :
Hierarchical inheritance is again similar to simple inheritance. Here, however, a single contract acts as a base contract for multiple derived contracts.
Multiple inheritance :
Solidity supports multiple inheritance. There can be multiple levels of single inheritance.
However, there can also be multiple contracts that derive from the same base contract.
These derived contracts can be used as base contracts together in further child classes.
Encapsulation :
Encapsulation is one of the most important pillars of OOP. Encapsulation refers to the process of hiding or allowing access to state variables directly for changing their state.
It refers to the pattern of declaring variables that cannot be accessed directly by clients and can only be modified using functions. This helps in constraint access to variables but, at the same time, allows enough access to class for taking action on it.
Solidity provides multiple visibility modifiers such as external , public , internal , and private that affects the visibility of state variables within the contract in which they are defined, inheriting child contracts or outside contracts.
Polymorphism :
Polymorphism means having multiple forms. There are the following two types of polymorphism:
1. Function polymorphism
2. Contract polymorphism
Function polymorphism
It refers to declaring multiple functions within the same contract or inheriting contracts having the same name.
The functions differ in the parameter data types or in the number of parameters. Return types are not taken into consideration for determining valid function signatures for polymorphism. This is also known as method overloading.*/
pragma solidity ^0.4.19;
contract helloFunctionPloymorphism {
function getVariableData(int8 data) public pure returns(int8 output) {
return data;
}
function getVariableData(int16 data) public pure returns(int16 output) {
return data;
}
}
// Contract polymophism
/*Contract polymorphism refers to using multiple contract instances interchangeably when the contracts are related to each other by way of inheritance. Contract polymorphism helps in invoking derived contract functions using a base contract instance */
pragma solidity ^0.4.19;
contract helloFunctionPloymorphism {
function getVariableData(int8 data) public pure returns(int8 output) {
return data;
}
function getVariableData(int16 data) public pure returns(int16 output) {
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment