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 4

31: Abstract Contract

An abstract contract is a special contract type in Solidity that provides a base contract from which other contracts can be derived. The purpose of an abstract contract is to provide various common and reusable pieces of code that can be used to create new contracts. This makes it easier for developers to create contracts that are more efficient and secure.

  • Example 1: A Simple Abstract Contract Below is an example of a simple abstract contract that contains a greet() function. This function can be used as a base for other contracts to build upon.

contract Abstract {  
  function greet() public virtual {    // Do something here  }
}
  • Example 2: An Abstract Contract with a State Variable Below is an example of an abstract contract with a state variable. The balance state variable can be used as a base in other contracts.

contract Abstract {  
  uint256 public balance;  
  function greet() public virtual {    // Do something here  }
}
  • Example 3: An Abstract Contract with a Getter Function Below is an example of an abstract contract with a getter function. The getBalance() function can be used as a base in other contracts.

contract Abstract { // abstract can be like an interface
  uint256 public balance;  
  function greet() public virtual;
  function getBalance() public view returns (uint256);
}

contract A is Abstract {  
  uint256 public balance;  
  function greet() public virtual {    // Do something here  } 
  function getBalance() public view returns (uint256) {    
    return balance;  
  }
}

That's it for the lesson 31! In the next lesson, Payable

Previous30: InterfaceNext32: Payable

Last updated 1 year ago