Inheritance Assignment

Excellent analysis, @dAnijboned! :+1:

1 Like

Hi @Thaddeus19,

Interesting choice of name — count — for the local variable which temporarily holds the owner’s address as a payable address. What was your reason for chosing this name? Do you think it will be clear to other developers what it’s purpose is?

By the way, what amendments did you make to contract HelloWorld in terms of its inheritance?

1 Like

Nice analysis, @zzzsoren :+1:

Also, check this post out — it explains how you can make an additional adjustment in terms of the inheritance. I think you’ll find it interesting.

1 Like

Nice solution, @ZAR :ok_hand:

By the way, what amendments did you make to contract HelloWorld in terms of its inheritance?

1 Like

Great analysis, @rbrownlow :+1:

So, just to confirm, I’ve understood that your HelloWorld contract now starts…

…and you’ve removed the code that explicity inherits contract Ownable , because HelloWorld now indirectly inherits Ownable via Destroyable. Is that right?

1 Like

Yes sir! That is exactly what I did. The code works the exact same for me. I tried it both ways!

1 Like

Yes, it will… but your solution is the more efficient one. It may well save on some gas as well (due to less code) but I’d need to check that… or maybe you could? :slightly_smiling_face:

2 Likes

Thank you :slightly_smiling_face:
So the HelloWorld contract implicitly inherits Ownable contract, by inheriting the Destroyable contract which inherits the Ownable contract.

1 Like

Exactly right @zzzsoren! :+1:

2 Likes
import "./ownable.sol";
contract Destroyable is Ownable {
function close() public onlyOwner { 
  selfdestruct(owner);  
}

}

1 Like

Hi @Jamie_Frame,

Your solution is correct, but it involves making an additional change to contract Ownable. Do you know what it is and why?

And what amendments did you make to contract HelloWorld in terms of its inheritance?

1 Like

Hi Jon,

Owner needs to be a payable address so that any funds associated with the detroyed contract can be sent to the owner address. HelloWorld needs to inherit from the Ownable and Destroyable contracts.

1 Like

contract HelloWorld is Ownable, Destroyable{...}

Created new contract Destoyable.sol:

import "./Ownable.sol";
pragma solidity 0.5.12;

contract Destroyable is Ownable {
    function close() public onlyOwner {
      selfdestruct(msg.sender);
    }
}
1 Like

I gave up in the end but I think it almost worked.

-----destroyable.sol--------
import “./2_Owner.sol”; -> built in owner.sol provided by remix.
pragma solidity >=0.4.22 <0.7.0;

contract Destroyable is Owner
{
function Destroy() public isOwner()
{
selfdestruct(msg.sender);
}
}

-----helloworld.sol--------
import “./2_Owner.sol”; -> built in owner.sol provided by remix.
import “./destroyable.sol”;

contract HelloWorld is Owner, Destroyable
…

function Kill() public isOwner
{
Destroy();//inherited from destroyable contract
}

1 Like

contract Ownable{
// this contract contains the ‘onlyOwner’ modifier and the instantiated ‘owner’ variable
}
…
new file
…
import “./Ownable.sol”;
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(owner);
}
}
…
new file
…
import “./Ownable.sol”; // Is this needed, given that Destroyable is Ownable?
import “./Destroyable.sol”;
contract HelloWorld is Destroyable {
}

1 Like

Hi @Jamie_Frame,

Yes :+1:

:ok_hand:… also, check this post out — it explains how you can make an additional adjustment in terms of the inheritance. I think you’ll find it interesting.

1 Like

Hi @sahowe1,

What was your reason for using the Owner contract provided by Remix? You should use the one that we built during the lesson:


You could use the template provided by Remix, but you would have to make some modifications. This may be one of the reasons why you couldn’t get yours to work.

Also, there is no need to add a function to HelloWorld which calls your Destroy function in Destroyable. This is duplication, as the Destroy function can already be called from Remix due to it being inherited and its public visibility. If you remove this additional function you’ve added in HelloWorld, and then deploy HelloWorld, you’ll see that this is the case. This is inheritance in action.

Yes I was getting a type cast error for address - I think one was payable and the other not. Thanks for the comments - all taken on board…

1 Like

Hi @FrankB,

You are absolutely right…we no longer need to import Ownable.sol.

This is also correct.
As Destroyable inherits Ownable we can remove the code from HelloWorld that explicity inherits Ownable. This is because HelloWorld now inherits from Destroyable, and so also indirectly inherits Ownable via Destroyable.
Wow!..that’s a mouthfull :sweat_smile:

Just one other observation…

This solution is correct, but it involves making an additional change to contract Ownable. Do you know what it is and why? We don’t need to make any changes to Ownable if we use msg.sender instead of owner , but using owner is perfectly valid, as long as Ownable is modified accordingly.

In the Owner contract provided by Remix the address owner has private visibility, whereas we want to have public (or at least internal) visibility. It is also a non-payable address.

By using msg.sender instead of owner in your Destroy function in Destroyable, you should have avoided any problems associated with this and that shouldn’t have thrown an error. But I imagine the error you got was in HelloWorld, because there we need access to owner (which we won’t have if it’s marked private in Owner).

I hope that makes sense, and fits in with what you’ve been getting. :slightly_smiling_face: