What is Inheritance in Solidity?

What is Inheritance in Solidity?

Let's take a closer inspection at Inheritance in Solidity. Inheritance is an advanced concept in #solidity and also in other languages, too. Inheritance is a way to extend the functionality of a contract - for example when you have something that belongs to you and then you want to give access to somebody else. Solidity supports both single as well as multiple inheritance, so you can inherit once or you can have it multiple.

The contract in Solidity is similar to a class in C++. In C++ you can inherit things. So you can inherit traits from a class and then in a contract. It further demonstrates the apparent similarities between C++ and Solidity. Classes can inherit and why not contracts, too?

How do we inherit? Let's take two contracts to explain this.

No alt text provided for this image

Now, if we want the SecondContract to inherit the information from the FirstContract we will use the keyword 'is'. Just like that, the second contract inherits the first contract.

No alt text provided for this image

Next, we will test it by taking an exercise and go through the solution together.

Exercise:

Create two contracts A and B

  1. Contract A should have a state variable called innerVal initialized to 100.
  2. Create a fully accessible function called innerAddTen which returns 10 to any given input.
  3. Contract B should inherit from contract A.
  4. Contract B should have a function called outerAddTen which returns the calculation from innerAddTen function in Contract A to any given input.
  5. Create a function in contract B which returns the value of innerVal from contract A

Begin with writing two contracts A and B. Contract A has a state variable that should be initialized to 100.

No alt text provided for this image

Also, in the first contract we have a fully accessible function (public) which returns 10 to any given input.

No alt text provided for this image

Next point Contract B should inherit from contract A.

No alt text provided for this image

First function is called outerAddTen which returns the calculation from innerAddTen function in Contract A to any given input.

No alt text provided for this image

And second function which returns the value of innerVal from contract A.

No alt text provided for this image

That is it. Let's deploy both contracts and look if everything is working.

No alt text provided for this image

See you in the next articles about #solidity , #cryptocurrencies , #smartcontract , #evm and #web3development .

I uploaded this code to my GitHub account.

要查看或添加评论,请登录

Ion Platon的更多文章

  • Storage vs Memory in Solidity

    Storage vs Memory in Solidity

    Understanding the storage and memory is not always intuitive. Having a good understanding of storage versus memory is…

  • What is Mapping in Solidity?

    What is Mapping in Solidity?

    If you are coming from other programming languages, then is good to know that the solidity mapping is similar to a…

  • Struct and Enum in Solidity

    Struct and Enum in Solidity

    A struct is a collection of key->value pairs similar to a mapping, but the values can have different types. Structs…

  • What are Libraries in Solidity?

    What are Libraries in Solidity?

    Libraries are similar to Contracts, but are mainly intended for reuse. A Library contains functions which other…

  • What are Interfaces in Solidity?

    What are Interfaces in Solidity?

    Interfaces are similar to abstract contracts and are created using interface keyword. There are some characteristics…

  • The Restricted Access Pattern in Solidity

    The Restricted Access Pattern in Solidity

    The Restricted Access to a Contract is a common practice. By default, a contract state is read-only, unless it is…

  • Build First Cryptocurrency in Testnet using Solidity and Remix

    Build First Cryptocurrency in Testnet using Solidity and Remix

    Creating own currency is a great way not only to solidify our coding or solidify our knowledge of building of smart…

  • The Fallback Functions in Solidity. How gas costs work in Smart Contracts.

    The Fallback Functions in Solidity. How gas costs work in Smart Contracts.

    Let's take a look at Fallback Functions in solidity. We are going to open our eyes and become a little bit more…

  • Bytes vs Strings in Solidity

    Bytes vs Strings in Solidity

    Strings are values that are made up of ordered sequences of characters, such as "hello world!" A string can contain any…

  • Loops in Solidity

    Loops in Solidity

    Looping is a type of functionality in coding, which is very handy. Loops allow you to iterate through data and take…

社区洞察

其他会员也浏览了