Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  1. STEP 3

26: Inheritance

Inheritance is a common OOP feature that allows a contract to build on the functionality of another contract. It is a powerful tool for code reuse and code organization, and allows us to build smart contracts with complex behavior.

In Solidity, inheritance works by means of a keyword, is, which is used to indicate that one contract inherits from another.

Let's look at an example of inheritance in Solidity:

contract A {    
    function foo() public {        // do something    }
} 

contract B is A {    
    function bar() public {        // do something else    }
}

Here, the contract B inherits from the contract A. This means that B will have access to the foo() function defined in A. In addition, it will also have its own bar() function.

Inheritance can be chained, meaning that a contract can inherit from another contract which itself inherits from yet another contract.

  • For example:

contract A {    
    function foo() public {        // do something    }
} 

contract B is A {    
    function bar() public {        // do something else    }
} 

contract C is B {    
    function baz() public {        // do something else    }
}

Here, the contract C inherits from the contract B, which in turn inherits from the contract A. This means that C will have access to the foo() and bar() functions defined in A and B, as well as its own baz() function.

Inheritance is a powerful tool for code organization and code reuse. It allows us to build complex smart contracts with clean and simple code.

That's it for the lesson 26! In the next lesson, Shadowing Effect

Previous25: Data LocationNext27: The Shadowing Effect

Last updated 2 years ago