17 votes
Accepted

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

Last question first: overloading concept applies to Solidity but, as for other programming languages supporting overloading functions, i.e. C++, they are not related to each other in any way. ...
Giuseppe Bertone's user avatar
15 votes

Constructors of inherited contracts in Solidity

No, you do not need to call the parent constructor yourself unless you need to pass arguments to it (detailed examples here). As far as addresses go, the solidity file specified above is basically ...
Jeff Coleman's user avatar
  • 22.1k
14 votes
Accepted

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

The assignment is as follows: first to the inherited variables, from the leftmost contract to the rightmost, finishing with the current contract you're in. In the following example C inherits from ...
matta's user avatar
  • 351
13 votes

Contract inheritance

The accepted answer by @shawn-tabrizi is a good example of how to set up inheritance properly but the reason you got the error is different and I want to share for anyone else that finds this answer ...
Justin Harris's user avatar
12 votes
Accepted

Multiple Inheritance and Linearization

After research I found: With multiple inheritance, there is an issue caused by the Diamond Problem. Solidity solves this problem like Python. Here are two links describing this problem in detail: ...
branko terzic's user avatar
12 votes
Accepted

How do I share an enum definition between contracts?

Enums defined inside a contract are by default accessible from other contracts. In Producer you can use the expressions: Supplier.State.inactive Supplier.State.active Supplier.State.kaput Your code ...
Jesbus's user avatar
  • 10.5k
12 votes

Composition Over Inheritance - Gas Efficiency

We have two approaches: Inheritance contract Parent { string w; function Set(string _w) public { w = _w; } function Greet(string name) internal view returns(string) { ...
Aquila's user avatar
  • 1,812
11 votes
Accepted

Override parent method and modifiers

Yes, you are correct. Something I noticed which is worth keeping an eye on is that both modifiers of 'parent' contracts as well as any functions are overwritten by inheritance even when using 'super'....
JasoonS's user avatar
  • 379
10 votes
Accepted

Interface Function

It's an "Abstract" a.k.a. "Interface" contract. It's used to define the interface, meaning the functions that exist, their exact names and inputs/outputs while being silent about how they work ...
Rob Hitchens's user avatar
  • 55.2k
9 votes
Accepted

What is 'is' keyword in Solidity

The is keyword (not "Is") is used for inheritance in Solidity. The closest thing to a list of keywords in the Solidity docs is the Cheatsheet which has: Global Variables Function Visibility ...
eth's user avatar
  • 85.7k
8 votes
Accepted

Solidity and multiple inheritance

I did a few test and seems like the order you use to extend the classes it matter. Given this example: pragma solidity 0.4.21; contract Ownable { event OwnableE(uint); function Ownable() ...
qbsp's user avatar
  • 4,367
8 votes

Providing base contracts' constructor arguments

In Solidity, you specify base constructor arguments in two ways (according to https://docs.soliditylang.org/en/latest/contracts.html#arguments-for-base-constructors): The first way to do it is more ...
iku's user avatar
  • 181
8 votes
Accepted

Contract inheritance

I believe this exact scenario is covered by the Solidity: Inheritance > Arguments for Base Constructors Here is the example they give: pragma solidity ^0.4.22; contract Base { uint x; ...
Shawn Tabrizi's user avatar
7 votes
Accepted

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

I see where you're going with this and why the struggle. In summary, inheritance isn't going to work out with this approach. The fundamental thing is the structs and mappings (etc) forever define the ...
Rob Hitchens's user avatar
  • 55.2k
7 votes

Multiple Inheritance and Linearization

Best explained in the solidity docs on Multiple Inheritance and Linearization: You have to list the direct base contracts in the order from “most base-like” to “most derived”. Note that this order ...
Paul Razvan Berg's user avatar
7 votes
Accepted

Difference between library-using-for and contract-is

In the first instance your contract is inheriting from the TestLibrary contract, and therefore you can perform the operation with increment(_base) or super.increment(_base). Additionally, if the ...
AndyK's user avatar
  • 695
7 votes

question regarding linearization of inheritance

This is an instance of the "Diamond problem" when a language supports multiple inheritance. It gets impossible to draw the inheritance graph. Solidity uses something called C3 linearization (https://...
Lauri Peltonen's user avatar
6 votes
Accepted

Use of function overriding in Solidity

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 ...
Achala Dissanayake's user avatar
5 votes
Accepted

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

It's just too tangled up to sort it out in a few moves. The main thing seems to be the way you're mixing up interfaces and inheritance. You don't want to use inheritance to merely talk to another ...
Rob Hitchens's user avatar
  • 55.2k
5 votes
Accepted

Call parent class modifier possible?

You could override an internal function that implements the check pragma solidity 0.4.24; contract Base { bool public active; modifier canSetMetadata() { require(canSetMetadata_()); ...
Henk's user avatar
  • 4,271
5 votes

Composition Over Inheritance - Gas Efficiency

In a 1-1 comparison Composition is more expensive to deploy, and execute. That said, if you need to deploy many instances, you could use the Library pattern and use composition in that way. Libraries ...
Steve Ellis's user avatar
  • 1,357
5 votes

Is library use inherited?

No. In Solidity 0.7.0 and later versions, the effect of using ... for is no longer inherited. Quoting from the 0.7.0 changelog: using A for B only affects the contract it is mentioned in. Previously, ...
Samuel Peter's user avatar
5 votes
Accepted

Underhanded Solidity: Exploiting multiple inheritance

The 'exploit' no longer applies to solc v0.6, since then the compiler generates an error when there's an ambiguous call. As the author says solc v0.5 uses C3 linearization to determine the order in ...
Ismael's user avatar
  • 30.6k
5 votes
Accepted

How to call a function that is inherited from another contract using Hardhat?

The problem was that safeTransferFrom is a overloaded function. Overloaded functions are functions that are defined more than once, with the same name, but they take different types and/or number of ...
Falcon Stakepool's user avatar
4 votes
Accepted

Is fallback function inheritable?

Using Browser-Solidity, I compiled and deployed contract B with the following code: contract A { function() { throw; } } contract B is A() { } I executed a sendTransaction(...) to B (...
BokkyPooBah's user avatar
  • 40.3k
4 votes
Accepted

Distinct, inconsistent compilation of inherited but identical contract in solidity

I do not have a full answer but I will share what I found out. I used Browser Solidity to compile contracts, and used Etherscan's disassembler to help me out with the result. When I compiled contract ...
Xavier Leprêtre B9lab's user avatar
4 votes
Accepted

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

This is touched on a bit here: https://solidity.readthedocs.io/en/v0.4.21/contracts.html#visibility-and-getters, but essentially what you're saying is correct. <address>.<function>() (e.g....
user19510's user avatar
  • 28k
4 votes
Accepted

msg.sender is Null in Base Constructor

A little clean-up. You need Test2 defined before Test1 inherits from it, so the order is reversed. Add public to functions to avoid warning about default visibility. Constructor in Test1 has ...
Rob Hitchens's user avatar
  • 55.2k
4 votes

Composition Over Inheritance - Gas Efficiency

From the perspective of gas usage, composition will be more expensive, mainly because you will have more CALL opcodes and more contracts instantiations. See Appendix G Fee Schedules (G_call, G_create, ...
ivicaa's user avatar
  • 7,519
4 votes

Inheritence in Solidity

acont and bcont are 2 separate contracts. They have nothing shared between them. In the Inheritance section in the docs it says that: Solidity supports multiple inheritance by copying code ...
Tudor Constantin's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible