Inheritance in Solidity

Inheritance in Solidity

Inheritance is one the important concept in solidity, Inheritance enables new contract to receive—or inherit—the properties and methods of existing contracts.

Below is one of the example where teacher contract inheriting member contract.

pragma solidity ^0.4.0;
contract member{
string name;
function setName(string _name){
name=_name;
}
}
contract teacher is member{
function getName() public returns(string){
return name;
}
}

 

There are three types of access which you can define in inheritance.

  • public
  • private
  • internal

Public:  Variables and functions will be accessible in child contract and as well as outside.

Private: Variables and functions will be accessible within parent smart contract only, neither in child contract nor  outside.

Internal: Variables and functions will be accessible within parent contract and as well as child contract but not outside.

Below are some of the facts about inheritance.

  • If a contract inherit multiple contracts, all of the code from parent contract get copied and
    single contract gets deployed on blockchain.
  • Internal keyword is used when we want to only allow access of variables in inherited contract.
  • Private keyword does not allow variables to accessed in inherited contract.

No Comments

Post a Reply

Inquire Now
close slider