13

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.

Physes
  • 1,308
  • 12
  • 18
  • 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 06 '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 06 '16 at 21:25
  • And why do you believe it applies in a different way than any other OOP language ? – Nicolas Massart Jun 06 '16 at 21:26
  • 4
    How is this not related to Ethereum? If someone asked about structs it wouldn't be a C-related question. – Physes Jun 06 '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 http://programmers.stackexchange.com/ and look at http://programmers.stackexchange.com/questions/108240/why-are-interfaces-useful – Nicolas Massart Jun 06 '16 at 21:29

1 Answers1

11

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.

dbryson
  • 6,403
  • 2
  • 27
  • 37