4

I have seen in countless places the terms treated synonymously. I have coded this example below which all works and I can see why this approach is useful. But is this an interface or an abstract contract?

Why do we even need interfaces?

 pragma solidity ^0.4.16;

contract d7 {
    function getBalance(address addr) pure public returns (uint) {
        d8 bg = d8(addr);
    return bg.myBalance();
    }
}

contract d8 {
    function myBalance() pure  returns (uint);

}

Then d8 -

pragma solidity ^0.4.18;

contract d8 {

    function myBalance() pure public returns (uint) {
        return 880;
    }

}

Hence I can leave d7 unchanged and redeploy d8 and then supply the new address to d7. This sounds a good idea to me. But I am unsure of the terminology to describe this contract and also why the interface keyword is used.

3

There are a number of differences between "interfaces" and "abstract contracts", although they can function in similar ways. Interfaces tend to be more abstract than abstract contracts, but neither of them will compile by definition.

From most to least specific: contract => abstract contract => interface

The "interface" keyword is the newer form, earlier versions of Solidity didn't have it and so older interfaces used "contract". Technically you can use either keyword for an interface, but using "interface" has clearer semantics.

The real difference lies in what you can/can't do in each type.

An interface cannot inherit other interfaces or contracts, and cannot implement ANY of its functions. In addition, an interface may not define a constructor, structs, enums or variables.

An abstract contract, on the other hand, has only 1 requirement, which is that at least 1 of the functions defined cannot be implemented.

Hope this was helpful!

2

From the Solidity 0.4.20 documentation,

Interfaces are similar to abstract contracts, but they cannot have any functions implemented. There are further restrictions:

  1. Cannot inherit other contracts or interfaces.
  2. Cannot define constructor.
  3. Cannot define variables.
  4. Cannot define structs.
  5. Cannot define enums.

Some of these restrictions might be lifted in the future.

Interfaces are basically limited to what the Contract ABI can represent, and the conversion between the ABI and an Interface should be possible without any information loss.

So I guess the main difference is that none of the functions in an interface can be implemented. In an abstract contract, you can implement some and as long as you leave at least one unimplemented it's still an abstract.

  • I get your comment, but I really wanted to determine why the keyword "interface" is used. I understand why we need "interface contracts" to allow for deployment without changing UIs. But the keyword interface seems to have no real utility to me. – Trevor Lee Oakley Mar 12 '18 at 8:33
  • I think from a compiler's perspective the interface keyword is technically pointless, it's one of those things which makes developers' lives easier because when you know it's an interface (especially one that you didn't write) you can automatically rule out having to deal with a bunch of things and it gives you a framework you can work to without having to worry about the guts. For example, the ERC20 spec is an interface, it doesn't care how your ERC20's "transfer" function works, as long as it follows these rules: function transfer(address to, uint tokens) public returns (bool success); – AnAllergyToAnalogy Mar 12 '18 at 8:56
  • I have seen dozens of examples of "interfaces" which do not use the word interface. The docs have just a few lines. If the developers want to use it, I would expect to see examples. I did not see any except the docs examples. – Trevor Lee Oakley Mar 12 '18 at 9:05
  • Remember Solidity is new and rapidly evolving. I don't think interface existed before v0.4.11, so the examples you're referring to are probably older than that. Or the people who wrote them just aren't up to date with what's available in v0.4.20 – AnAllergyToAnalogy Mar 12 '18 at 9:21
  • You know I just realised the ERC20 standard on the Ethereum Wiki doesn't even use it... Perhaps it will be deprecated in a future version. – AnAllergyToAnalogy Mar 12 '18 at 9:24
1

Abstract Contracts :

Contracts can lack function implementation that is called abstract Contract, These contracts will not compile but can be inherited and implemented, a contract derived from an abstract contract without all the methods implemented is also an abstract contract.

Interface Contracts :

Defined with the keyword Interface. Similar to an abstract contract but is stricter than that. - It cannot implement any of the functions. - Cannot Inherit another contract - No enums - No variables - No Structs - No Constructors

It basically limited to the contracts ABI represents so "conversion between ABI and Interfaces can be done without any data loss".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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