-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: inherited state variables declared
- Loading branch information
1 parent
808aaa6
commit 794a082
Showing
1 changed file
with
31 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,33 @@ | ||
import Callout from 'nextra-theme-docs/callout' | ||
|
||
# Inherited State Variables | ||
# Inherited State Variables | ||
- Unlike functions, state variables cannot be overridden by re-declaring it in the child contract. | ||
|
||
```jsx | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.13; | ||
|
||
contract A { | ||
string public name = "Contract A"; | ||
|
||
function getName() public view returns (string memory) { | ||
return name; | ||
} | ||
} | ||
|
||
// Shadowing is disallowed in Solidity 0.6 | ||
// This will not compile | ||
// contract B is A { | ||
// string public name = "Contract B"; | ||
// } | ||
|
||
contract C is A { | ||
// This is the correct way to override inherited state variables. | ||
constructor() { | ||
name = "Contract C"; | ||
} | ||
|
||
// C.getName returns "Contract C" | ||
} | ||
``` |