Questions tagged [inheritance]

Inheritance in this stackexchange refers to parent child contract relationship where the child contract is considered to be a type of the parent contract and hence implement the parent contract functions. This is same like inheritance in OOP in generic programming except for the fact that in this community its used in a contract oriented programming domain.

Filter by
Sorted by
Tagged with
25
votes
2answers
9k views

'internal' keyword in a function definition in Solidity

Does the internal keyword in a function definition such as: function doSomeThing(uint _param) internal {...} work similar to designating a method as private in other languages and thus makes the ...
14
votes
1answer
4k views

Inheritance and Function Overwriting: Who can call the parent function?

I want to know who is still able to call an original overwritten function in case of inheritance. Let's assume I have the following setup: contract A{ address public owner; function A(){ ...
9
votes
2answers
3k views

Constructors of inherited contracts in Solidity

Given: contract A { address owner; function A() { owner = msg.sender; } } contract B is A { string greeting; function B(string _greeting) { greeting = _greeting; } } When I ...
9
votes
2answers
927 views

Solidity and multiple inheritance

I'm attempting to understand how solidity resolves multiple inheritance. As explained in the docs, Solidity uses C3 linearization. The relation between my classes is displayed on the image below: I ...
7
votes
3answers
234 views

Composition Over Inheritance - Gas Efficiency

In OOP languages, composition over inheritance is a well-known best practice. Solidity is an OOP language too but there is also the gas efficiency issue. Question is, in Solidity, how do composition ...
7
votes
1answer
159 views

Distinct, inconsistent compilation of inherited but identical contract in solidity

Consider the following solidity source file A.sol: pragma solidity ^0.4.8; contract A {} and B.sol: pragma solidity ^0.4.8; contract A {} contract B is A {} Using solc version 0.4.8, the code ...
6
votes
2answers
3k views

Multiple Inheritance and Linearization

Hello to everyone. I am looking at multiple inheritance in Solidity. I came across this example within their documentation, but it is not understandable enought for me. Can someone explane this ...
5
votes
1answer
2k views

Override parent method and modifiers

For example, we have a contract: contract A { modifier checkCaller() { require(msg.sender == 0x123); _; } function doSmth() checkCaller {} } We make child contract and ...
5
votes
1answer
2k views

What is 'is' keyword in Solidity

When I read contract A Is B can this be read as contract A extends from contract B ? Is is a mechanism for inheriting behavior, this is equivalent to objects inheriting behavior in object oriented ...
5
votes
1answer
2k views

Use of function overriding in Solidity

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 ...
5
votes
1answer
882 views

How to implement a counter (variable that is updated every time the contract is called) in Solidity?

Parent and child contract are: contract test{ uint a ; function test() { a = 1; } function val() returns(uint){ return a; } } contract test2 is test{ uint b = a++...
4
votes
3answers
4k views

Interface Function

I understand that the following function is used to link the ico contract to the token. Appreciate if someone could explain how to use the function, and exactly how it works. Thank you. /** * @title ...
4
votes
1answer
1k views

How do I share an enum definition between contracts?

I have two related contracts, let's call them Supplier and Producer. A Supplier can have a State of inactive, active, or kaput, but only active suppliers can be added to a Producer. So something ...
4
votes
1answer
172 views

Call parent class modifier possible?

I want to do the following: contract Base { bool public active; modifier canSetMetadata() { require(active == false); _; } } contract BaseWithMetadataControl is Base { ...
4
votes
1answer
869 views

Overwriting of constant variables by inheritance: Why are parent variables only overwritten in functions of the child?

Let's assume I have the following two contracts Hello and Goodbye, and Goodbye inherits from Hello. Both declare a constant variable aString, i.e. the child contract Goodbye overwrites the parent's ...
4
votes
1answer
248 views

Is fallback function inheritable?

I just have no means of testing it out myself right now. contract A() { function() { throw; } } contract B is A() {}
4
votes
1answer
79 views

In Solidity, how does the slot assignation work for storage variables when there's inheritance?

In a simple solidity contract, without inheritance, storage variables get assigned sequentially starting from slot 0. But in case of inheritance, the inherited variables appear before or afterwards?
4
votes
1answer
768 views

solc returns empty bin for contracts with constructor and inheritance

Having a simple contract structure like this: pragma solidity ^0.4.8; contract A { uint public a1; } contract B is A { uint8 public b1; } I can compile it with: > solc --combined-json "abi,...
4
votes
1answer
486 views

What is the execution order of Inherited constructors?

I'm reading the section on inherited constructors on the documentation here. The example given confuses me than understanding the concept, and it doesn't explain the most important part, the ...
3
votes
1answer
1k views

Providing base contracts' constructor arguments

I'm a bit puzzled about multiple inheritance and providing arguments for different constructors. Solidity documentation has long explanations about this (http://solidity.readthedocs.io/en/develop/...
3
votes
1answer
1k views

Difference between library-using-for and contract-is

I am using two different ways to get the same result and wondering what the real difference is. Both options (library, using, for and contract, is)deliver just an extended code from a file. Method no ...
3
votes
3answers
472 views

Contract inheritance? Need some help to understand certain things

I'm using testrpc, and truffle. I created a simple token as seen in many tutorials which shall be used to buy or enable certain actions when clicking a button on an UI. contract MyToken { mapping ...
3
votes
1answer
498 views

Multiple inheritance and order of chaining of multiple base constructors [duplicate]

I am trying to understand the order of invocation of base class constructors in the presence of multiple inheritance. As far as I can tell, the order appears under-specified in the Solidity ...
3
votes
1answer
2k views

(Solidity) Inheritance: how to add additional attributes to a struct in base contract?

This is a very simplified example but it of course extends to more complicated contracts too, I just haven't found an elegant solution. contract Group { struct Person { uint age; ...
3
votes
1answer
189 views

Struct inheritance or workaround

I've got a quite complex contract with two arrays: struct TransactionsLink { uint transactionId; uint creationTime; uint amount; } mapping( address => TransactionsLink[]) public ...
2
votes
2answers
1k views

Using contract from another contract

I have deployed one contract named Storage using truffle console code for Storage.sol pragma solidity ^0.4.8; contract Storage { struct EntityStruct { string str_address; uint entityData; ...
2
votes
1answer
198 views

msg.sender is Null in Base Constructor

When I create the child class object and try to access value of msg.sender from parent class it always comes 0. Can anyone please help me in this? Below is the sample code: contract Test1 is Test2 { ...
2
votes
1answer
303 views

Calling parent method with multiple inheritance

I have the following contract definition: contract MyToken is StandardToken, Ownable { ... function transfer(address _to, uint256 _value) public returns (bool){ require(...); // would ...
2
votes
1answer
83 views

A Solidity Linearization Puzzle

I was playing in remix just to concretize my understanding of Solidity linearization, and got a result I didn't expect and can't rationalize. Why does AC_BA.go() return 3? (Please scroll to the ...
2
votes
2answers
277 views

Trying to reference mortal.sol in standard ethereum library

I am trying to reference mortal.sol in the standard ethereum library. I have deployed mortal.sol under my main test account. However I get the error source not found. What I tried was: import "https:/...
2
votes
2answers
596 views

How to access Objects from different Contracts

Goal: I want to create a solidity programm that includes 3+ contracts. This means I have one contract (lets call it master for now) which aims at setting everything up. This means this contract has a ...
2
votes
1answer
38 views

Solidity inheritance

Do contracts have to implement the methods they inherit from interfaces, likewise, do they have to implement methods from abstract contracts?
2
votes
1answer
55 views

What order is code copied for inheritance?

The Solidity documentation says it supports inheritance by copying code, but is unclear about where the code is copied to. I am building a genesis block for a Quorum-based chain, starting from the ...
2
votes
2answers
840 views

Gas estimation failed in Remix

Can't really use the transfer function as it is written. It assumes the owner uses it. So made a new function for the "minting" function for the faucet to use. Not sure why the compiler message is so ...
2
votes
1answer
79 views

possible to implement abstract base function with property in derived contract?

Is there any way to have an abstract base contract with an abstract function which gets implemented in a derived contract by a property (and not a function)? e.g. the following attempts failed: ...
1
vote
2answers
491 views

Contract inheritance

I really hope this is not a dumb question. I am trying to call the constructors of the inherited contracts in a structure like this: contract A{ uint a; constructor (uint _a) public { ...
1
vote
2answers
83 views

Inheritence in Solidity

I want to implement my contract in much better way and want to use the inheritence. I wrote the code and i face one problem that i want to access the mapping variable of Contract A in the Contract B. ...
1
vote
2answers
24 views

Is it possible to have an optional property or method in an interface?

I currently can't deploy any contract which doesn't adhere 100% to the specified interface(s). Is there a way to mark some properties or methods as optional and, if not, is this a planned feature?
1
vote
1answer
64 views

If I import Ownable and MyClass inherits from it, do I need to migrate the Ownable at all?

I'm a bit confused about migrations. I've just been told to use 1_migration.js file per contract, except maybe in the case of imports. I am going to change these migrations back to one per migration, ...
1
vote
1answer
226 views

Does “this” keyword changes msg.sender on inherited methods invocations?

Take this scenario: Contract A { function whoIsSender() public returns (address){ return (msg.sender); } } Contract B is A { function withThis() public returns (address){ return ...
1
vote
2answers
171 views

Solidity, is it possible to deploy a smart contract that prevents other smart contracts from inheriting from it?

I am new to Ethereum and I was wondering if it is possible to deploy a smart contract such that other smart contracts are not able to inherit from it. In other words once a smart contract is deployed ...
1
vote
1answer
889 views

TypeError: Definition of base has to precede definition of derived contract

In my DAPP I have 3 contracts where I want to do something like this. pragma solidity ^0.4.13; import "./z2.sol"; contract z1 { uint example; function z1 (){ example = 33;} function ...
1
vote
1answer
43 views

Value of msg.sender in call to inherited external function

I found this in the official solidity docs. The values of all members of msg, including msg.sender and msg.value can change for every external function call. This includes calls to library ...
1
vote
1answer
73 views

Solidity inheritance can't read parent's value correctly

I found a strange error in my contract and tried to resolve it, but it's so unreasonable. contract parentContract { uint num = 1; function setNum(uint _n) public returns(uint) { num ...
1
vote
1answer
46 views

Solidity Syntax question

What happens if a function is being passed before { into another function? Like DetailedERC20(_name, _symbol, TOKEN_DECIMALS) in this example: function MyToken(Registry _registry, string _name, ...
1
vote
1answer
15 views

Do over-ridden functions affect dependent parent functions?

Imagine a ParentContract with functions a() and b(), where b() calls a() contract ParentContract{ function a() {...} function b() { a() } } Now, I have a Child contract that ...
1
vote
1answer
161 views

Help with inheritance and Truffle tests

I have two contracts that both inherit from OpenZeppelin's Whitelist. After deploying both contracts, I use an instance of the first contract to add a "trusted" address to the whitelist. Since both ...
1
vote
1answer
66 views

question regarding linearization of inheritance

If I have the following inheritance: pragma solidity ^0.4.24; interface A { function foo(uint256 _x) external pure returns(uint256); } contract B1 is A { function foo(uint256 _x) public ...
1
vote
1answer
58 views

Contract Inheritence

Let's suppose we created two contracts: Contract A and Contract B. Contract A is inherit by Zeppelin's standard token ERC20 where as Contract B is inherit by contract A. Will contract B be ...
1
vote
1answer
63 views

Multiple inherintance: solidity docs example explanation

I found this example on the docs: pragma solidity ^0.4.22; contract owned { constructor() { owner = msg.sender; } address owner; } // Use `is` to derive from another contract. Derived // ...