1

Let's say contractA has a special function. And made an interface that contains the special function without implementation. To use the function in the other contractB, we can use the interface but also we can just inherit contractA or call the function.

Wouldn't it be more convenient if you simply inherit the contract or call from outside?

3 Answers 3

2

Just to try to clarify a bit more: if a contract has already been deployed, you can't inherit it. That's where you mostly need interfaces.

Even if you are the one deploying the code you should be careful about inheritance - don't use it unless you know that the child contract requires all the functionality from the parent. Interfaces are useful also for separation of concerns.

2

We need interface in Solidity because interface provides a reusable and customizable approach to programming smart contracts.

Yes, as in your example, to use the function in the other contractB, we can use the interface but also we can just inherit contractA or call the function. However, if you use inheritance, you need to override the method because you're using an implementation that has already been made. If you use interface, you code your own implementation to fit the logic in your applications. However, although inheritance has common uses in Solidity, libraries in Solidity cannot be inherited if you want to use the functions in the libraries.

1
  1. import/inheritance: Contract A (another contract) and B (your contract) should be both deployed. When you call a function of Contract A from Contract B, you are not changing the state of Contract A. You are changing state of Contract B. In other words, you think that you are calling the function of Contract A but in reality, you are calling a function of your contract (B) which is copied from contract A.

  2. call: Contract A (another contract) and B (your contract) should be both deployed. When you call a function of Contract A from Contract B, you are changing the state of Contract A. In other words, you are really calling the function of Contract A. And for that reason, you need pay a gas fee if you change the state of the Contract A. You can think this, like calling a function of any contract by using your Metamask account.

  3. interface: Contract A (another contract) and B (your contract) should be both deployed. Interfaces are used for standardization. Let's say that Contract A is created by following a certain interface (a standard set of functions). And it also has some extra functions. You do not know those extra functions. And also you do not need them. But you know those standard functions ("interface functions") and you need them. In this case, you can create a variable on Contract B. In this variable, save contract A as an instance (You will need address of contract A). And the type of the variable will be the interface name. For that, you need to import the file which includes that interface. Then you can access the functions of Contract A. But be careful here, because you can access only those standard ("interface") functions of Contract A. You cannot access other functions of Contract A. And lastly, by calling those functions, you can change the state of Contract A.

So, to answer your question, call method is used to access a specific function of Contract A. interface is used to access a standard function of Contract A. Both of them can change the state of Contract A.

import/inheritance does not call the functions of Contract A. The functions and variables of Contract A, are copied to Contract B. So, you are calling your own (Contract B's) functions in this case.

Your Answer

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

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