New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Inheritance

BaseMon, Oct 9, 2023, 12:00 AM

Inheritance in Solidity: Learn how to reduce code and create clear, maintainable smart contracts by abstracting functionalities into smart units. By the end, you'll be able to make your contracts more organized and efficient, taking your Ethereum projects to the next level! 🚀💡 📚 Head to Base Camp for full courses on smart contract development: https://docs.base.org/base-camp/docs/welcome ✅ Subscribe to our channel here: https://www.youtube.com/@BuildonBase ► Build on Base: https://base.org ► Follow us on Twitter: https://twitter.com/buildonbase ► Join our Discord: https://base.org/discord

Transcript

in this exercise we are going to learn about inheritance in solidity inheritance is a very important concept of object-oriented programming and one of the advantages is the ability to reduce code and Abstract your smart contracts in a smart units that are responsible of specific actions doing the right absorption will help you to create clear and maintainable and smart contracts alright let's see what solidity can offer Us in regards to inheritance and now we are going to create a new file called inheritance.soil okay now let's copy the pragma and the license and let's simply say contract and we're gonna call it contract a okay now let's create another contract called contract B okay and what we're gonna do now is we're gonna create two functions function that is going to call who am I okay this is going to be external and he's gonna return a string and the string is gonna say contract B okay and let's copy this function and we're gonna add it also in contract a this function is pure and the same all right now let's test here I have selected the Contra 8 and we just deploy we call who am I and his contract a now contract B and then contract B okay all right now what are we gonna do is we are gonna delete this function and we're gonna say Contra a is contract B by doing this we are inheriting all the methods that are specified in the contract B okay now let's test I have selected a contra a and I simply deploy and then who am I contract B because the functionality is here however solidity is going to create one single contract and it's gonna bring all the functionality of contract B okay now what we're gonna do is we are going to create a function here called who am I externa okay and this is gonna be external pure and the same is going to return a stream memory okay now let's also create another function call who am I internal and this is gonna be also internal all right who am I external he's gonna call who am I internal thanks to the inheritance because by inheriting we are able to access all the functionality in the contract B even if this function is internal however we can simply say here return who am I internal and that's all okay let's test okay I'm deploying and now I have the who am I external and who am I so let's call who am I and he's calling this one and now who am I external is also accessing this function okay so this is a great way to reduce functionality from the contract parent okay

Automatic transcript — names and jargon may be misspelled.