0

Just a random thought and a question. Can a smart contract written in solidity be inherited and if so, How?

1 Answer 1

1

Sure. Just like this:

contract Foo {
  ...
}

// Contract Bar inherits contract Foo
contract Bar is Foo {
  ...
}

Actually, one contract may inherit several contracts:

contract Foo {
  ...
}

contract Bar {
  ...
}

// Contract Zoo inherits contracts Foo and Bar
contract Zoo is Foo, Bar {
  ...
}

See documentation for details.

2
  • Okay, this is fine but what if one of my contract is already deployed on a network and now I am writing another contract and I want to inherit that contract. Is that possible?
    – vteja711
    Nov 6, 2019 at 9:11
  • 1
    Sure, in case you have source code of the first contract, just import it and inherit from it. Your question is similar to the following C++/Java question: “May I inherit from a class, if I already created an instance of that class?”. Surely you can. Nov 6, 2019 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.