2

I wanted to make sure I understood the purpose of interface contracts. I understand that they are contracts with unimplemented functions, but wanted to make sure I understood all the ways programmers use them practically. As I understand it, smart contract programmers create contracts that inherit from interface contracts so:

  1. They can refer to interface contract files to make sure their implementations of the functions within it have the same input and output types. However, type congruity is not actually enforced because any functions the programmer implements in the child smart contract overrides the unimplemented functions in the interface (which are implicitly virtual).

  2. They can ensure all functions included in the interface contract are implemented by them. If the programmer forgets to implement all functions in the child contract they created, they have accidentally created an abstract contract and an error will be raised.

Is this essentially how interface contracts are used?

1 Answer 1

1

In Object Oriented Programming (OOP), interfaces act as a 'contract' that the implementing classes need to forcibly implement. It let us know what our class is and allows us to use the concept of Polymorphism.

In Smart Contracts, interfaces are used for the same purpose; specify the structure of a smart contract, and allow polymorphism (since we can create multiple different contracts that implement the same interface and behave differently, but still use them through the interface). Using interfaces in our smart contracts is useful because we don't have to have a copy of the smart contracts we are going to use beforehand.

For example, the following code does not use interfaces, but the Vet contract wants to treat multiple animals the same way (don't worry about this, it's just a simple example). So, without interfaces, it has to have all the references of the contracts it needs to work with and compile with them. It will not be able to add any other animals in the future because smart contracts are immutable, and it has to have specific functions and duplicated logic, for each animal:

contract Dog {
    function woof() external {
        // do something related to dog
        // woof
    }
}

contract Cat {
    function meow() external {
        // do something related to dog
        // woof
    }
}

contract Vet {

    function treatDog(address _dogAddress) public {
        Dog dog = Dog(_dogAddress);
        dog.woof();
    }

    function treatCat(address _catAddress) public {
        Cat cat = Cat(_catAddress);
        cat.meow();
    }

}

But with interfaces, the Vet contract can dynamically treat any animal without knowing the details of its implementation. The new animal just has to implement the IAnimal interfaces and the Vet contract can certainly work with it, dynamically, thanks to polymorphism:

interface IAnimal {
    function makeSound() external;
}

contract Dog is IAnimal {
    function makeSound() external {
        // do something related to dog
        // woof
    }
}

contract Cat is IAnimal {
    function makeSound() external {
        // do something related to cat
        // meow
    }
}

contract Vet {

    function treatAnimal(address _animalAddress) public {
        // Casting the _animalAddress address to the `IAnimal` interface reference, to polymorphically handle any animal.
        // If _animalAddress belongs to a Cat, then animal.makeSound will sound like a cat, if it belongs to a bear, then it will make a bear sound, and so on.
        IAnimal animal = IAnimal(_animalAddress);
        animal.makeSound();
    }

}

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.