Solidity
Inheritance
Notes are from the Reference:
Solidity Documentation
https://media.readthedocs.org/pdf/solidity/latest/solidity.pdf
Inheritance
• Solidity supports multiple inheritance by copying code including
polymorphism.
• All function calls are virtual, which means that the most derived
function is called, except when the contract name is explicitly given.
• Even if a contract inherits from multiple other contracts, only a
single contract is created on the blockchain, the code from the base
contracts is always copied into the final contract.
• The general inheritance system is very similar to Python’s,
especially concerning multiple inheritance.
– https://docs.python.org/3/tutorial/classes.html#inheritance states
the following:
– class DerivedClassName(Base1, Base2, Base3): For most purposes, in the simplest
cases, you can think of the search for attributes inherited from a parent class as depth-
first, left-to-right, not searching twice in the same class where there is an overlap in the
hierarchy. Thus, if an attribute is not found in DerivedClassName, it is searched for in
Base1, then (recursively) in the base classes of Base1, and if it was not found there, it
was searched for in Base2, and so on.
– Note: in Solidity it is from right-to-left
pragma solidity ^0.4.0; // Multiple inheritance is possible. Note that "owned" is
// also a base class of "mortal", yet there is only a single
contract owned { // instance of "owned" (as for virtual inheritance in C++).
function owned() { owner = msg.sender; } contract named is owned, mortal {
address owner; function named(bytes32 name) {
} Config config = Config(0xd5f9d8d94886e70b06e474c3fb14fd43e2f23970);
NameReg(config.lookup(1)).register(name);
// Use "is" to derive from another contract. Derived }
// contracts can access all non-private members including
// internal functions and state variables. These cannot be // Functions can be overridden by another function with the same name and
// accessed externally via `this`, though. // the same number/types of inputs. If the overriding function has different
contract mortal is owned { // types of output parameters, that causes an error. Both local and message-
function kill() { // based function calls take these overrides into account.
if (msg.sender == owner) selfdestruct(owner); function kill() {
} if (msg.sender == owner) {
} Config config = Config(0xd5f9d8d94886e70b06e474c3fb14fd43e2f23970);
NameReg(config.lookup(1)).unregister();
// It is still possible to call a specific
// These abstract contracts are only provided to make the // overridden function.
// interface known to the compiler. Note the function mortal.kill();
// without body. If a contract does not implement all }
// functions it can only be used as an interface. }
contract Config { }
function lookup(uint id) returns (address adr);
} // If a constructor takes an argument, it needs to be provided in the
// header (or modifier-invocation-style at the constructor of the derived contract
contract NameReg { contract PriceFeed is owned, mortal, named("GoldFeed") {
function register(bytes32 name); function updateInfo(uint newInfo) {
function unregister(); if (msg.sender == owner) info = newInfo;
} }
function get() constant returns(uint r) { return info; }
uint info;
}
pragma solidity ^0.4.0;
contract main {
function hello() constant public returns (uint) { return(0); }
}
contract A is main {
function hello() constant public returns (uint) { return(1); }
}
contract B is main {
function hello() constant public returns (uint) { return(2); }
}
contract C is main {
function hello() constant public returns (uint) { return(3); }
}
contract D is A, B, C {
}
Note: hello() called from D returns 3.
pragma solidity ^0.4.0;
contract main {
function hello() constant public returns (uint) { return(0); }
}
contract A is main {
function hello() constant public returns (uint) { return(1); }
}
contract B is main {
function hello() constant public returns (uint) { return(2); }
}
contract C is main{
function hello() constant public returns (uint) { super.hello() ; }
}
contract D is A, B, C {
}
Note: hello() called from D returns 0.