-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path034-abstract-contracts.sol
53 lines (45 loc) · 1.43 KB
/
034-abstract-contracts.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Abstract Contracts
* @notice Abstract contracts is one which contains a least one
* function without any implementation.
*
* Generally an abstract contract contains both implemented
* and abstract functions.
*
* Derived contract will implement the abstract function and use
* the existing functions as they are and when required.
*
* Here we talk of two things
* 1. base contract and
* 2. the derived contract
*/
// this will act as out base contract
abstract contract LearnMoreOnEvents {
// since the function has no implementation we can mark it as virtual
function sendMessage() public view virtual returns (string memory) {}
}
// derived contract
contract DerivedContract is LearnMoreOnEvents {
// overriding the method within the base contract
function sendMessage() public pure override returns (string memory) {
return "Hello World!";
}
}
// technically the contract is still abstract since it has atleast one
// funtion without an implementation.
contract Member {
string name;
uint256 age = 25;
function setName() public virtual returns (string memory) {}
function getAge() public view returns (uint256) {
return age;
}
}
// derived contract
contract Teacher is Member {
function setName() public override pure returns (string memory) {
return "TheUnicornDev237";
}
}