5

I am trying to learn from other contracts and have seen an example which I do not understand. In this example a first contract is defined as follows, but it seems that the functions do not actually do anything. (BTW this is the etherdelta smartcontract...)

contract Token {

  //part of the contract functions goes here

  function transfer(address _to, uint256 _value) returns (bool success) {}

  // other part of the contract functions goes here
}

Then, a new contract is defined, which seems to refer to the first contract:

contract StandardToken is Token {

  function transfer(address _to, uint256 _value) returns (bool success) {
    //Default assumes totalSupply can't be over max (2^256 - 1).
    //If your token leaves out totalSupply and can issue more tokens as time goes on, you need to check if it doesn't wrap.
    //Replace the if with this one instead.
    if (balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]) {
    //if (balances[msg.sender] >= _value && _value > 0) {
      balances[msg.sender] -= _value;
      balances[_to] += _value;
      Transfer(msg.sender, _to, _value);
      return true;
    } else { return false; }
  }

Can anybody explain me please what this does?

1 Answer 1

6

Contract standard Token is derived from Token. This concept is called inheritance.

Solidity as a contract-oriented programming language allows inheritance that enables new contracts to take on the properties of existing contracts.

Functions can be overridden by another function with the same name and the same number/types of inputs.

Here the transfer function in Token is overridden in it's derived contract StandardToken.

This is useful for polymorphism, where you can define different concrete implementations for different uses, but still give a same interface for all.

In the above posted code, contract token defines a function transfer but doesn't implement the behavior of it. But all the other types of tokens (including standardtoken here and may be some other) can be made to derived from token contract and inside them they implement the behavior specific to them accordingly. Users will have to only know there is a function called transfer to use any type of token contracts derived from token thus providing a common interface.

Refer here as well.

6
  • Many thanks for the quick answer, which explains it perfectly. What I have not understood though is why they do it like this. Oct 24, 2017 at 6:50
  • that's for the purpose of polymorphism basically, that allows different concrete implementations of the same abstract interface. Oct 24, 2017 at 6:53
  • Many thanks. No, I am just a hobby programmer and I have not come across this concept before. Oct 24, 2017 at 7:00
  • No ;-) But I will try to look it up and see what it means. Oct 24, 2017 at 7:02
  • you are welcome , refer this question as well :) Oct 24, 2017 at 7:18

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.