Solidity - Abstract Contract - GeeksforGeeks

11 May, 2022
Abstract contracts are contracts that have at least one function without its implementation. An instance of an abstract cannot be created. Abstract contracts are used as base contracts so that the child contract can inherit and utilize its functions. The abstract contract defines the structure of the contract and any derived contract inherited from it should provide an implementation for the incomplete functions, and if the derived contract is also not implementing the incomplete functions then that derived contract will also be marked as abstract. In Solidity there is no abstract keyword for creating an abstract contract, a contract itself becomes abstract if it has unimplemented functions.
Example: In the below example, an abstract contract is created which is inherited by another contract that has implemented all the functions of the abstract contract. Abstract contract instance is created in the calling contract and an object of child contract is created. Using the object of child contract all the abstract functions that are implemented in the child contract are invoked.
Solidity
// Solidity program to // demonstrate // Abstract contractpragma solidity 0.4.19;  // Creating an abstract contractcontract abstractContract {      // Declaring functions    function getStr(      string _strIn) public view returns(      string memory);    function setValue(uint _in1, uint _in2) public;    function add() public returns(uint);}  // Child contract inheriting // an abstract parent // contract 'abstractContract'contract derivedContract is abstractContract{      // Declaring private     // variables    uint private num1;    uint private num2;      // Defining functions inherited     // from abstract parent contract    function getStr(      string _strIn) public view returns(      string memory){        return _strIn;    }          function setValue(      uint _in1, uint _in2) public{        num1 = _in1;        num2 = _in2;    }    function add() public returns(uint){        return (num2 + num1);    }      }  // Caller contractcontract call{      // Creating an instance of     // an abstract contract    abstractContract abs;          // Creating an object of     // child contract    function call(){        abs = new derivedContract();    }      // Calling functions inherited     // from abstract contract    function getValues(    ) public returns (uint){        abs.setValue(10, 16);        abs.getStr("GeeksForGeeks");        return abs.add();    }      }
 
Output :  
Here, abstractContract contract is an abstract contract that has some functions without their implementation, and derivedContract is its child contract. The getStr() and setValues() functions takes string and values, while the add() function adds the values of setValues() function. abs is an object of abstractContract which creates an instance of derivedContract in call contract that uses the variables of the contract and calls the functions.
Blockchain
Solidity
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Storage vs Memory in Solidity
Ethereum Blockchain - Getting Free Test Ethers For Rinkeby Test Network
How to connect ReactJS with MetaMask ?
How to Become a Blockchain Developer?
Proof of Work (PoW) Consensus
Storage vs Memory in Solidity
Solidity - Inheritance
Mathematical Operations in Solidity
How to Install Solidity in Windows?
Introduction to Solidity