The Basics of Smart Contracts 6: Abstraction, Inheritance and Interfaces
- andy1265
- Aug 2, 2022
- 2 min read
Much like other modern object orientated programming languages Solidity supports abstraction, inheritance and interfaces. The concepts in this installment will be familiar to anyone with experience of other object orientated languages as they are implemented in very similar ways.
Inheritance
Inheritance is one of the core concepts behind object orientated programming. Inheritance is a mechanism that allows one class to derive features from another class for a hierarchy of classes with a common set of attributes and methods.
We covered inheritance briefly in our previous article when discussing constructors from inherited contracts. We can quickly view how to define one contract that inherits from another using the same code snippet which can be seen below:
contract parent {
string private message;
constructor(string memory _message){
message = _message;
}
function getMessage() public view returns(string memory){
return message;
}
}
contract child is parent {
constructor(string memory _message) parent(_message){}
}Here we can see the child class inherits from parent. Following on from this we can set the value of message (a parameter within parent) in Childs constructor and retrieve it through an instance of child.
Interfaces
Interfaces are very similar to contracts however with interfaces all of the functions are defined but none are implemented. When inheriting from an interface the contract must implement all the interfaces functions. Interface functions can also only be set as public.
Unlike in other languages interfaces in Solidity can inherit from other interfaces but can not inherit other contracts. A basic example of an interface can be seen below:
interface interfaceExample {
function setMessage(string memory _message) public;
function getMessage() public view returns (string memory);
}Abstract Contracts
Abstract contracts are very similar to interfaces except that some functions will be defined and others will not. The definition for an abstract contract is almost exactly the same as for a normal contract however the compiler will mark a contract as abstract if not all of its methods are defined.
You may also define state variable and complex type variable in abstract contracts. Generally abstract contracts are used for defining basic functionality to be utilized by numerous other contracts.
contract abstractContractExample is interfaceExample {
string private message;
function setMessage(string memory _message) public {
message = _message;
}
function getMessage() public view returns (string memory);
}
The above code demonstrates how to turn the functionality from our previous 2 examples into an abstract contract. This has the setMessage function completed and the getMessage function as just the definition. This contract inherits from our previous interface example.
Challenges
At this point we should be fairly comfortable with the deployment of these contracts using javascript and hardhat. If you are not go back and read the previous installments (particularly the first 2). As such we will leave the deployment and testing of these contracts to you this time.
Our challenges are a little less guided this time:
Write a contract that inherits from abstractContractExample (remember this also inherits from interfaceExample).
Add the functionality to your class to get the keccak256 hash of the value of message.
Deploy your contract to a local block chain.
Retrieve the keccak256 hash of message from your deployed contract using the calling mechanism of your choice.

Comments