Solidity - Basics of Interface Last Updated : 09 Mar, 2023 Comments Improve Suggest changes 5 Likes Like Report Interfaces are the same as abstract contracts created by using an interface keyword, also known as a pure abstract contract. Interfaces do not have any definition or any state variables, constructors, or any function with implementation, they only contain function declarations i.e. functions in interfaces do not have any statements. Functions of Interface can be only of type external. They can inherit from other interfaces, but they can't inherit from other contracts. An interface can have enum, structs which can be accessed using interface name dot notation. Example: In the below example, the contract MyContract implements an interface from the InterfaceExample.sol file and implements all the interface functions. Import the InterfaceExample.sol file into the MyContract.sol file before deploying it. InterfaceExample.sol : Solidity // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.6 <0.9.0; //initialize the interface interface InterfaceExample{ // Functions having only // declaration not definition function getStr( ) external view returns(string memory); function setValue( uint _num1, uint _num2) external; function add( ) external view returns(uint); } MyContract.sol: Solidity // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.6 <0.9.0; /// @title A contract for demonstrate the working of the interface /// @author Jitendra Kumar /// @notice For now, this contract just show how interface implements in the smart contract import "./InterfaceExample.sol"; // Contract that implements interface contract MyContract is InterfaceExample{ // Private variables uint private num1; uint private num2; // Function definitions of functions // declared inside an interface function getStr() public view virtual override returns(string memory){ return "GeeksForGeeks"; } // Function to set the values // of the private variables function setValue( uint _num1, uint _num2) public virtual override{ num1 = _num1; num2 = _num2; } // Function to add 2 numbers function add( ) public view virtual override returns(uint){ return num1 + num2; } } contract call{ //Creating an object InterfaceExample obj; constructor(){ obj = new MyContract(); } // Function to print string // value and the sum value function getValue( ) public returns(string memory,uint){ obj.setValue(10, 16); return (obj.getStr(),obj.add()); } } Output : Create Quiz Comment J jeeteshgavande30 Follow 5 Improve J jeeteshgavande30 Follow 5 Improve Article Tags : Programming Language Solidity Blockchain Explore Solidity Tutorial 6 min read Solidity BasicsIntroduction to Solidity 5 min read Setting Up Smart Contract Development Environment 5 min read Solidity - Basic Syntax 5 min read "Hello World" Smart Contract in Remix-IDE 4 min read Solidity - Comments 4 min read Solidity - Types 4 min read Variable and OperatorsSolidity - Variables 4 min read Solidity - Variable Scope 3 min read Solidity - Operators 7 min read Control Flow in SoliditySolidity - While, Do-While, and For Loop 3 min read Solidity - Decision Making Statements 4 min read Solidity - Break and Continue Statements 2 min read Reference & Mapping Types in SoliditySolidity - Strings 3 min read Solidity - Arrays 6 min read Solidity - Enums and Structs 3 min read Solidity - Mappings 4 min read Solidity - Conversions 6 min read Solidity - Ether Units 7 min read Solidity - Special Variables 3 min read Solidity - Style Guide 13 min read Solidity FunctionsSolidity - Functions 4 min read Solidity - Function Modifiers 8 min read Solidity - View and Pure Functions 2 min read Solidity - Fall Back Function 3 min read Solidity Function Overloading 1 min read Mathematical Operations in Solidity 6 min read Solidity AdvancedSolidity - Basics of Contracts 4 min read Solidity - Inheritance 6 min read Solidity - Constructors 4 min read Solidity - Abstract Contract 3 min read Solidity - Basics of Interface 2 min read Solidity - Libraries 4 min read Solidity - Assembly 4 min read What are Events in Solidity? 2 min read Solidity - Error Handling 6 min read Top 50 Solidity Interview Questions and Answers 15+ min read Like