Skip to content

Instantly share code, notes, and snippets.

@mickys
Created May 6, 2019 16:40
Show Gist options
  • Save mickys/5e209e09c6fd82aaf935a7450e1b0bbe to your computer and use it in GitHub Desktop.
Save mickys/5e209e09c6fd82aaf935a7450e1b0bbe to your computer and use it in GitHub Desktop.
Careful when you override / redeclare the same variable in a child contract
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();
}
}
@Solexplorer
Copy link

Very helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment