1

Im trying to implement my first interface. But when I try to add a new variable, Remix tell me that can´t add variables in interfaces.

It have sense to set the fixed variables that the extended solidity files are going to have?? Or I´m making a concept mistake??

pragma solidity ^0.4.24;
interface testI {
    uint id;
    function addI(uint _n) external;
}
1

Some concepts:

An abstract contract implements only some of the declared functions, while leaving the others unimplemented. When choosing to use an abstract contract, it is typically because we have a group of contracts with a common functionality, but with additional unique functionality in each contract. So we implement this common functionality in the abstract contract, and we can therefore declare in this contract any state variable required for the implementation of this functionality.

An interface is a set of declared functions, none of which is implemented. When choosing to use an interface, it is typically because we have a group of contracts with a common set of functions, but with a different purpose (and therefore a different implementation) in each contract. Since none of the functions declared in the interface is implemented, state variables are kind of "inappropriate" in this scope, and the language does not permit them. In addition, all functions must be declared external.

1

Interfaces are used when you want to interact with contracts already deployed on the blockchain. You cannot have constructor or variables inside interface contract, only abstract methods (functions without bodies). Interfaces are also used when you want to write upgradeable contracts where state and logic are separated in eternity and proxy contract so here the proxy to be able to use eternity contract methods we need interface contract.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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