DEV Community

Cover image for Abstract Contracts in Solidity
Shlok Kumar
Shlok Kumar

Posted on

Abstract Contracts in Solidity

Abstract contracts facilitate patterns like the template method, which improves code readability and generalizes our code.

Abstract contracts in Solidity are a powerful tool for developers to create smart contracts that can interact with other contracts and applications. Abstracts allow developers to define interfaces between different parts of their applications, allowing them to keep the code clean and organized. They also provide an easy way for developers to extend existing functionality without having to write new code from scratch.

Contracts that include at least one function but do not include its implementation might be referred to as abstract contracts. It is not possible to generate an instance of an abstract. Abstract contracts are typically utilized as base contracts, allowing child contracts to inherit and make use of the functions of the parent contract. Any derived contract that is inherited from the abstract contract 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. The abstract contract defines the structure of the contract, and any derived contract that is inherited from the abstract contract should provide an implementation for the incomplete functions. In Solidity, there is no abstract term that may be used to create an abstract contract. Instead, a contract is considered abstract if it contains functions that have not been implemented.

An abstract contract is a contract that cannot be deployed by itself. It must be inherited by another contract. An abstract contract can help us generalize a contract and avoid code duplication. They facilitate patterns like the template method and improve code readability. To define an abstract contract in Solidity, we use the abstract keyword before the contract keyword. An abstract contract can contain both implemented and unimplemented functions. The unimplemented functions are declared with only their function signature, without any implementation.

Example 1 -

// Abstract Contract
abstract contract MyAbstractContract {
    function myFunction() public virtual returns (uint);
}

// Inherited Contract
contract MyInheritedContract is MyAbstractContract {
    function myFunction() public override returns (uint) {
        // Implementation of myFunction()
        return 42;
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we define an abstract contract MyAbstractContract with an unimplemented function myFunction(). We then define another contract MyInheritedContract that inherits from MyAbstractContract. We implement the myFunction() function in MyInheritedContract. Note that the virtual keyword is used to indicate that the function can be overridden by child contracts. The child contracts must use the override keyword to indicate that they are overriding a parent's virtual function. In conclusion, abstract contracts in Solidity allow us to generalize our code and avoid duplication. They also facilitate patterns like the template method and improve code readability.

Example 2 -

pragma solidity ^0.8.0;

contract absContract {

     // Declaring functions
     function getStr (
     string strIn) public view returns(
     string memory);
     function setValue(uint in1, uint in2) public;
     function add() public returns(uint);
}


contract derived is absContract{
     // 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 contract
contract call{

     // Creating an instance of
     // an abstract contract
     absContract 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("Blockchain with Solidity");
           return abs.add();
     }
     
}
Enter fullscreen mode Exit fullscreen mode

In this example, an abstract contract is developed, and then it is inherited by a different contract that has completely implemented all of the abstract contract's functionalities. In the calling contract, an instance of an abstract contract is generated, and simultaneously, an object of the child contract is made. All of the abstract functions that are implemented in the child contract can be invoked, and they are accessed through the object of the child contract.

For more content, follow me at - https://linktr.ee/shlokkumar2303

Top comments (0)