// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
// Define a contract named ParentContract
contract ParentContract {
// Declare an internal state variable of type uint
// Internal variables can be accessed by this contract and contracts that inherit from it
uint internal simpleInteger;
// Public function to set the value of simpleInteger
// Any account can call this function to update simpleInteger's value
function SetInteger(uint _value) public {
simpleInteger = _value;
}
// Virtual public view function to return a uint value
// Marked as virtual so it can be overridden by inheriting contracts
// This specific implementation always returns 10, regardless of simpleInteger's value
function GetInteger() virtual public view returns (uint) {
return 10;
}
}
// Define a contract named ChildContract that inherits from ParentContract
contract ChildContract is ParentContract {
// Override the GetInteger function to return the actual value of simpleInteger
// This allows the ChildContract to provide its own behavior for GetInteger
function GetInteger() override public view returns (uint) {
return simpleInteger;
}
}
// Define a contract named Client
contract Client {
// Instantiate a ChildContract and assign it to a variable of type ParentContract
// Demonstrates polymorphism where a parent type variable is used to hold a child type object
ParentContract pc = new ChildContract();
// Public function to interact with the pc instance
// Sets the simpleInteger value and then retrieves it using GetInteger
// The behavior of GetInteger will be determined by the actual type of pc, which is ChildContract
// Hence, it will return the value of simpleInteger set by SetInteger
function WorkWithInheritance() public returns (uint) {
pc.SetInteger(100); // Set simpleInteger to 100
return pc.GetInteger(); // Should return 100 due to the override in ChildContract
}
}
//Deploy: