Created
May 6, 2019 16:40
-
-
Save mickys/5e209e09c6fd82aaf935a7450e1b0bbe to your computer and use it in GitHub Desktop.
Careful when you override / redeclare the same variable in a child contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract Base { | |
string name = "Parent Name"; | |
constructor() public { | |
} | |
function getName() public view returns (string memory) { | |
return name; | |
} | |
function setName(string memory newName) public { | |
name = newName; | |
} | |
} | |
contract Child is Base { | |
// overriding this is a very bad idea | |
string name = "Child Name"; | |
// setName() will work with super.name | |
// getName() will work with super.name | |
function getChildName() public view returns (string memory) { | |
return name; | |
} | |
function getParentName() public view returns (string memory) { | |
return super.getName(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very helpful