Reading The DAO and DigixDAO source code I notice that in contract systems there are often interface contracts where functions are declared without any body.

For example,

contract TokenInterface {
    function balanceOf(address _owner) constant returns (uint256 balance);
    function transfer(address _to, uint256 _amount) returns (bool success);

What is the point of interface contracts? The above contract in The DAO is followed by a contract Token is TokenInterface {} which repeats the exact functions declared in the interface contract except followed by the full function logic. Not sure what the point of this is- it seems redundant.

  • Your question seems to be more general OOP (Object Oriented Programming) question like "what is an Interface" than a Solidity specific question. Am I wrong ? – Nicolas Massart Jun 6 '16 at 21:23
  • 2
    No it's a Solidity question. I noticed TokenInferface while reading TheDAO source code. I'm interested specifically in how it applies to the Solidity language. – Physes Jun 6 '16 at 21:25
  • And why do you believe it applies in a different way than any other OOP language ? – Nicolas Massart Jun 6 '16 at 21:26
  • 3
    How is this not related to Ethereum? If someone asked about structs it wouldn't be a C-related question. – Physes Jun 6 '16 at 21:26
  • I guess we assume that basic knowledge about programming is required to ask for Solidity question. this is my own opinion, perhaps other user won't vote for my flag. I suggest to discuss the relevance of this question in the Ethereum Meta site. You could also ask on programmers.stackexchange.com and look at programmers.stackexchange.com/questions/108240/… – Nicolas Massart Jun 6 '16 at 21:29

Interface contracts work like other interface agreements in traditional object oriented languages. In your example, the interface specifies the functions a contract of type TokenInterface must implement to fit into the overall application framework.

This allows you to customize code without having to implement everything from scratch. For example, in the case of TokenInterface you could change/customize the logic for the 2 functions balanceOf and transfer and still use the rest of the framework.

The main point of using interfaces ( or Abstract Contracts ) is to provide a customizable and re-usable approach for your contracts.

  • This feature is called "Protocol" in other languages. – Vanja Dev Apr 3 '18 at 10:05
  • it could be a really great answer if you can give us an example :) – Ender Jul 31 '19 at 7:13

Your Answer

