Open In App
Related Articles

Solidity – Abstract Contract

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Abstract contracts are contracts that have at least one function without its implementation or in the case when you don’t provide arguments for all of the base contract constructors. Also in the case when we don’t intend to create a contract directly we can consider the contract to be abstract. 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. An abstract contract is declared using the abstract keyword. 

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 the child contract is created. Using the object of the child contract all the abstract functions that are implemented in the child contract are invoked.

Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
/// @title A contract for describes the properties of Abstract Contract
/// @author Jitendra Kumar
/// @notice For now, this contract just show the functionalities of Abstract Contract
abstract contract AbstractContract {
    // Declaring functions
    function getStr(
      string memory _strIn) public view virtual returns(
      string memory);
    function setValue(uint _in1, uint _in2) public virtual;
    function add() public virtual returns(uint);
}
 
// child contract 'DerivedContract' 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 memory _strIn) public pure override returns(
      string memory){
        return _strIn;
    }
     
    function setValue(
      uint _in1, uint _in2) public override{
        num1 = _in1;
        num2 = _in2;
    }
    function add() public view override returns(uint){
        return (num2 + num1);
    }
}
 
// Caller contract
contract Call{
 
    // Creating an instance of an abstract contract
    AbstractContract abs;
     
    // Creating an object of child contract
    constructor(){
        abs = new DerivedContract();
    }
 
    // Calling functions inherited from abstract contract
    function getValues(
    ) public returns (string memory,uint){
        abs.setValue(10, 16);
        return (abs.getStr("GeeksForGeeks"),abs.add());
    }
}

                    

 

Remix IDE

 

Deploy the Smart Contract: After successful compilation of the smart contract select the Call  contract before deploying it.

Select the Call Smart Contract

Output: To get the output of the smart contract click the getValues button and see the output in console log.

decoded output     {
                 "0": "string: GeeksForGeeks",
                 "1": "uint256: 26"
                 }

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.



Last Updated : 30 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads