Inheritance & Visibility

Welcome to the thread about Inheritance & Visibility in Ethereum. Here you can discuss everything about this chapter.

For me The Internal functions are hard to understand completly! but if i understood a little bit, can i continue? THX

1 Like

http://solidity.readthedocs.io/en/develop/contracts.html#visibility-and-getters

If I have understood :

visibility = who will be able to access our functions ?

public ( by default): anyone can access to the contract , outside and within the contract
private: only from the contract with this level of visibility ( not even for the children contract)
internal: only within the contract and by its children contract
external: only from outside the contract

5 Likes

That’s correct. Good job!

2 Likes

“Kennel is Dog” doesn’t feel right at all. Kennel is composed of Dogs in real life.

“Dog is Animal” would be better in order to avoid confusion about inheritance and composition.

3 Likes

That’s a good point. That would have been a better example of a real-life inheritance situation. If we update the course in the future I will take this into account. Thank you!

1 Like

Hey Filip, thanks for answering my last question in a timely manner… I have another (of course)…

I have been thinking that the use of dogToOwner mapping may not be as clear to read or understand for all the other developers who will be updating my code in the future [smiling]. I think a better and clearer way to code this dogToOwner mapping would be to just add owner as a property in the dog struct and skip the mapping completely. What do you think?

1 Like

Good question. The issue with not having a mapping at all is that it gets way more difficult to find a specific dog that is owned by a specific owner. If you only have the owner as a property in the struct, you would have to look at every single dog in order to find which is yours. This is very costly and slow. Instead we can use mappings, either dogToOwner or OwnerToDog, to find a dog or an owner very quickly. I hope this answered your questions. Or maybe you had thought about a clever solution?

2 Likes

Thanks for the response @filip … No clever Idea yet… but I am starting to think about how I could use mapping and msg.sender in a way that would help people who have lost their crypto private key. One of my goals for taking this course was to come up with an idea for a dapp. Maybe this is one… we’ll see.

3 Likes

I’m sure you’ll write lots of them, Dapps that is.

2 Likes

you should make a summary after each topic :wink:

i have a question about transferDog. is it correct that you can only transfer the last dog that was added to the dogs array? so lets say i run the code, add three dogs, then use the transferDog function, this will only transfer the last dog added correct? so the way the function has been made seems a bit strange to me

Not really. The dogs are not stored in an array. They are stored in a mapping. So every owner points to a dog. What is true however is that we can only store one dog per owner.

1 Like

Hi Filip, the dogContract code in the link does not compile properly…

the fix is to remove line 17: addedDog(owner, _name, id);
and to remove the ‘internal’ keyword from the addDog function

Cheers!

1 Like

Hi all,

is there a reason why ‘mappings’ are used to link the owner to the Dog, rather than say add an ‘owner’ field to the ‘struct’ and put the msg.sender address there?

Thanks, Mark.

Another question…

when I make the DogContract ‘addDog’ function internal, and I compile, the addDog function disappears. It still works on the Kennel Contract when I add in the addKennelDog function though, but I’m wondering why it cant been seen from within the DogContract itself?

Thanks, Mark.

Thank you! I have fixed it now. The internal keyword should still be present. But the addedDog event was a copy/paste error on my part.

You would still need to save the struct somewhere. Maybe you where thinking of storing the struct in an array instead of a mapping? That is also possible. But it wouldn’t be very effective when we are trying to find a certain dog. We would have to search through the entire array. With a mapping we can very effectively find a dog that belongs to a certain owner.

That’s because the function is no longer public. The standard visibility is public, meaning it can be executed from anywhere, including from remix. When we put the internal visibility on a function, we can only call the function from the contract itself or the derived contract.

Hi filip,
I am following your course above. Now, stuck at Import files message"Unable to import “browser/DogContract.sol”: File not found". could you please help me out.
Below are the code copied from you:

pragma solidity^0.4.0; //Unable to import “browser/DogContract.sol”: File not found

import “./DogContract1.sol”;
contract Kennel is DogContract1 {
function transferDog(address _newOwner) returns (uint) {
address owner = msg.sender;
uint dogId = ownerToDog[owner];
delete(ownerToDog[owner]);
ownerToDog[_newOwner] = dogId;
} //at this stage the auto compiler should by ok as in your tutorial but not my Remix
function addKennelDog(string _name, uint _age) {
addDog(_name, _age);
}
}
contract DogContract1{
struct Dog{
string name;
uint age;
}
Dog[] dog;
mapping (address =>uint) ownerToDog;
function Doglist (string _name, uint _age){
address owner = msg.sender;
uint id =dog.push(Dog(_name, _age)); // for mapping add "uint id = ".
ownerToDog[owner] = id;
}
function setDogList () returns (string) { //uint in setDogList() is replaced by codes below
address owner = msg.sender;
uint id = ownerToDog[owner];

     return dog [id-1].name;      
 }  
}