-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24_MultipleInheritance.sol
63 lines (49 loc) · 1.59 KB
/
24_MultipleInheritance.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/*
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.
*/
contract FirstParentContract{
// We add virtual keyword to indicate that this function can be overriden by the child contract
function greeter() external virtual pure returns (string memory){
return "Hello, I am First Parent Contract";
}
}
contract SecondParentContract{
event Logging(string message);
}
contract FirstChild is SecondParentContract{
function displayHello() external returns(string memory){
emit Logging("I am first child");
return "Hello";
}
}
contract SecondChild is SecondParentContract{
string public childName; // stores the name
constructor(){
childName = "Second Child";
}
}
// Note: The Inheritance Order must have the most base like contract name first
// followed by derived like.
// Here, the most base like is SPC (as it has no other inherited contracts)
// contract FirstGrandChild is SecondChild , SecondParentContract. (This will give error) ❌
contract FirstGrandChild is SecondParentContract,SecondChild{
constructor(){
childName = "First Grand Child";
}
function hello() external returns(string memory){
emit Logging("I am first grand child");
return "Hello";
}
}
/* Note:
1. Order of Inheritance
FPC SPC
↗ | ↖
FC | SC
FGC
where FPC: First Parent Contract , and so on.
*/