my question is simple and I want to know if there is a possibility to achieve something like this. I have two contracts, one inherit from the other; I want to access to a method of the child contract from the parent contract, i have clear how to do it in the other way (from child to parent).

Here is more clear:

contract A {

  function doSomethingA() {
   // Here I want to execute doSomethingB();


contract B is A {

  function doSomethingB() {
    // Here is what I want to execute from A

  • 2
    Not specifically Ethereum only, but this is just an object-oriented programming principle - contract A (parent) cannot call contract B (child) method via inheritance. Inheritance does not add the child method to the parent. The parent can only call a child method if it has a reference to an instance of child and the child method is not private. (But I do not know how a reference to another contract instance can be available within Solidity code) Jan 23 '18 at 22:53
  • Actually, in the case of Ethereum, the "reference to instance of child" is just the address and ABI of that contract and the way to call its method is to send it a transaction. My question here would be - why do you want to do this? If function doSomethingA() needs to execute doSomethingB(), I would just put function doSomethingA() inside contract B Jan 23 '18 at 23:03
  • Well, is because my dapp have an structure of more than contract A and B, and in some interactions, contract A acts like a child contract and serves the function to the functionality of the others contracts, so I want an elegant solution in order to not repeat code, but maybe override the function is the only solution
    – maoooricio
    Jan 24 '18 at 11:15

This sounds like you really just need to refactor your contracts and perhaps have a third contract (C) which both inherit from. Alternatively you could create a library.

// common functionality here
contract C {

  doThing1() {}

  doThing2() {}

contract A is C {

  function doThing1() {}


contract B is C {

  function doThing2() {}


You need parent contract’s address inside child contract. Then it is the same process.

This article would give you some good insights. https://hackernoon.com/ethereum-smart-contracts-lifecycle-multiple-contracts-message-sender-e9195ceff3ec

  • Where can I set that data? In the constructor of the child contract?
    – maoooricio
    Jan 24 '18 at 12:58
  • You cannot set at constructor as you would not know the address one of the contracts at the time creating the other. You would have to call a method in contract to set the address of the other.
    – Chim
    Jan 24 '18 at 13:02

I don't know how something of this kind can be possible in Solidity language because this can be possibe in Java language by Casting the method call using Child Class Reference.

But there is would be an issue in Solidity even if it would have been possible because this may lead to Deadlock condition since your child class method calls parent Class method and vice versa and this may result in all the gas been consumed.

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.