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.

5
  • 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 ? Jun 6, 2016 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, 2016 at 21:25
  • And why do you believe it applies in a different way than any other OOP language ? Jun 6, 2016 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 6, 2016 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/… Jun 6, 2016 at 21:29

1 Answer 1

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.