Inheritance & External Contracts - Discussion

Please share your code in the following way so we can review it and help you solve the issue:

Carlos Z

I had managed to solve it, there was a problem with the compiler. Thank you.

  1. The base contract is the same as the parent contract, while the derived contract is the same as a child contract.

  2. All functions on the base contract are available in the derived contract.

  3. Hierarchical inheritance is a phenomenon where a single contract act as a base contract for multiple contracts.

2 Likes
  1. What is the base contract? It is the parent contract.
  2. Which functions are available for derived contracts? All the functions of the parent contract.
  3. What is hierarchical inheritance? Multiple child contracts from the same parent contract.
2 Likes

Why does my contract not show the owner tab after I deploy the contract with the key word ‘public’?

This is the parent contract (right?) saved in another file:
contract Ownable {

address public owner;

modifier onlyOwner {

    require(msg.sender == owner);

    _;

}

constructor() {

    owner = msg.sender;

}

}

and i’ve used:
import “./ownable.sol”;
in my Hellowworld.sol Bank contract but I still only get: depost, transfer, withdraw and getBalance as button options in my result.

for reference here is my contract:

pragma solidity 0.7.5;

import “./ownable.sol”;

contract Bank is ownable{

mapping(address => uint) balance;

event depositDone(uint amount, address indexed depositedTo);



function deposit() public payable returns (uint)  {

    balance[msg.sender] += msg.value;

    emit depositDone(msg.value, msg.sender);

    return balance[msg.sender];

}



function withdraw(uint amount) public onlyOwner returns (uint){

    require(balance[msg.sender] >= amount);

    balance[msg.sender] -= amount;

    msg.sender.transfer(amount);

    return balance[msg.sender];

}



function getBalance() public view returns (uint){

    return balance[msg.sender];

}



function transfer(address recipient, uint amount) public {

    require(balance[msg.sender] >= amount, "Balance not sufficient");

    require(msg.sender != recipient, "Don't transfer money to yourself");

   

    uint previousSenderBalance = balance[msg.sender];

   

    _transfer(msg.sender, recipient, amount);

           

    assert(balance[msg.sender] == previousSenderBalance - amount);

}



function _transfer(address from, address to, uint amount) private {

    balance[from] -= amount;

    balance[to] += amount;

}

Any thoughts?

Many thanks

Could you please provide the code properly in the following way? The code you provided is not entirely in a good syntax.

Carlos Z

  1. the base contract is the parent contract
  2. functions of derived contracts are; single inheritance ,multi-level inheritance, hierarchical inheritance, multiple inheritance, encapsulation,polymorphism, and function polymorphism.
    3.a single contract acts as a base contract for multiple derived contracts
1 Like

Hi Ben17,

I ran into the same issue.

What you need to check is to make sure you are using the correct GovernmentInterface(address).

Make sure you copy the address in the deployed contract:
govtaddress

And put it in this line of your Bank contract:

Make sure you are not copying and using the address from the account drop down list with the 10 addresses to pick from.

That’s what corrected the same error message for me.

Hope that helps.

Hi justkaz,

I ran into the same issue.

What you need to check is to make sure you are using the correct GovernmentInterface(address).

Make sure you copy the address in the deployed contract:
govtaddress

And put it in this line of your Bank contract:

Make sure you are not copying and using the address from the account drop down list with the 10 addresses to pick from.

That’s what corrected the same error message for me.

Hope that helps.

1 Like

Destroyable contract


// SPDX-License-Identifier: MIT
import "./Ownable.sol";
pragma solidity 0.7.5;

//Allow any contractcontract inheriting from it to self destruct.
//This action should only be available for contract owner.
//keep contract owner functionality seperate in Ownable


contract Destroyable is Ownable {

    function destroyable() external onlyOwner  {

        //self destruct takes in 1 input, an addy to send all ether in contract.
        //Address(msg.sender) must be payable to work because it has to be capable of accepting eth.
        selfdestruct(payable(msg.sender)); 

    }

}

Ownable Contract

// SPDX-License-Identifier: MIT

pragma solidity 0.7.5;

contract Ownable{

    address public owner ;

modifier onlyOwner{

        require(msg.sender == owner);

        _; // run function

}

   constructor(){

        owner= msg.sender;

    }

// a tiny function placed to run before the main function code.
// If you have a piece of code that is going to be used alot for different fxns.
// require(msg.sender == owner), is called with modifier onlyOwner
//add onlyOwner to fxn after visibility.
   
}

Main Bank Contract

// SPDX-License-Identifier: MIT

pragma solidity 0.7.5;

//To iherit from a diff contract(ownable)

//1.import contract, 2. contractA is contractB{},3. add modifier from contract B to contract A function(withdraw)

import "./Ownable.sol";
import "./Destroyable.sol";
        
    
//Multiple inheritance start with most baselike(ownable), destroyable has prop of ownable.

contract Bank is Ownable, Destroyable{
1 Like

After reading the article https://betterprogramming.pub/solidity-what-happens-with-selfdestruct-f337fcaa58a7,
I still don’t understand the test methods store() and retrieve().
Why do we need to store and retrieve values, and these tests? What’s the purpose of the tests? What if we don’t do them?

  1. The parent contract is the base contract.
  2. All public and internal scoped functions are available inside derived contracts.
  3. Hierarchical inheritance is similar to simple inheritance. A single contract acts as a base contract for multiple derived contracts.
1 Like
  1. What is the base contract? The Base Contract is the parent contract that all other related contracts inherit from.
  2. Which functions are available for derived contracts? All functions are availible to derieved contracts
  3. What is hierarchical inheritance? hierarchical inheritance in a form of inheritance that has the base contract with multiple contracts inheriting from the base.
1 Like

1 The Base contract is the contract from which another contract inherits functions and variables. is the first in the chain and he does not inherit anything from other contracts.

2 all types of function and variables may be derived from child contracts

3 multiple child contracts inheriting from the same base contract. The contracts should follow a specific order while inheriting, starting from the base contract through to the most derived contract.

1 Like

i don’t get the difference between let owner variable be public or owner variable be internal.
we made a function in the “helloworld” contract that makes owner public from internal, what is the difference from leaving it directly public into the parent contract?

1 Like
  1. What is the base contract?
    the parent contract is known as a base contract

  2. Which functions are available for derived contracts?
    state variables and internal methods .

  3. What is hierarchical inheritance?
    a single contract acts as a base contract for multiple derived contracts.

1 Like

image

External Contract Quiz - I didn’t quite get this last one.
Could someone share a light/explain why is that or link any other posts that may have asked the same thing?
(tryed looking for it on the general search but the result wasn’t that specific)

1 Like
  1. What is the base contract?
    Base Contract is a contract that is inherited, which is the parent contract.
  2. Which functions are available for derived contracts?
    Public and internal functions are available for derived contracts.
  3. What is hierarchical inheritance?
    Hierarchical inheritance is when a single contract act has the base contract for multiple contracts.
  1. What is the base contract?

The contract that is inherited is called the parent contract and the contract that inherits is called the child contract.
Similarly, the contract has a parent known as the derived class and the parent contract is known as a base contract.
Inheritance is mostly about code-reusability. There is a relationship between base and derived contracts and all public and internal scoped
functions and state variables are available to derived contracts.

  1. Which functions are available for derived contracts?

Inheritance is mostly about code-reusability. There is a relationship between base and derived contracts and all public and internal scoped
functions and state variables are available to derived contracts. Solidity compiler copies the base contract bytecode into derived contract bytecode. The is keyword is used to inherit the base contract in the derived contract.

  1. What is hierarchical inheritance?

Hierarchical inheritance is again similar to simple inheritance. Here, however, a single contract acts as a base contract for multiple derived contracts.

  1. What is the base contract?
    The parent contract
  2. Which functions are available for derived contracts?
    public and internal
  3. What is hierarchical inheritance?
    Multiple derived contracts inheriting from the same base contract