Files
web/apps/base-docs/base-learn/docs/inheritance/abstract-contracts-sbs.md
Brian Doyle 566e5f5832 Rename Base Camp to Base Learn (#649)
* Rename Base Camp to Base Learn

* Rename Base Camp to Base Learn

* Update learn link

* change wallet type property from camel to snake case (#643)

* Update api key requirement, minor style updates (#642)

* Fix type and clarify inheritance ex (#655)

* Document Reth snapshot URLs (#651)

* feat(ecosystem): New additions to Ecosystem page (#647)

* feat(ecosystem): New additions to Ecosystem page

* chore(Ecosystem): Add image for Dynamic

* Update preparing-for-fault-proofs-on-base-sepolia.md (#633)

Update preparing-for-fault-proofs-on-base-sepolia

* Fix conflict

* fix conflict

---------

Co-authored-by: Brendan from DeFi <brendan.forster@coinbase.com>
Co-authored-by: Danyal Prout <danyal.prout@coinbase.com>
Co-authored-by: wbnns <hello@wbnns.com>
Co-authored-by: Olexandr Radovenchyk <radole1203@gmail.com>
2024-07-23 17:44:11 -04:00

2.2 KiB

title, description, hide_table_of_contents
title description hide_table_of_contents
Abstract Contracts Learn how to make contracts that must be inherited by another contract. false

Abstract contracts can't exist on their own. Their functionality can only be utilized by a contract that inherits from them. In this lesson, you'll learn how to create an abstract contract.


Objectives

By the end of this lesson you should be able to:

  • Use the virtual, override, and abstract keywords to create and use an abstract contract

Abstract Contracts

Continue with your Inheritance.sol file. Add ContractD as an abstract contract. Add a virtual function called whoAreYou function, but do not add any implementation for that function.

Reveal code
abstract contract ContractD {
    function whoAreYou() public virtual view returns (string memory);
}

Inheriting from an Abstract Function

Update ContractA to inherit from ContractD.

You'll get a slightly confusing error that ContractA needs to be marked as abstract. Doing so is not the correct fix.

from solidity:
TypeError: Contract "ContractA" should be marked as abstract.
  --> contracts/Inheritance.sol:25:1:
   |
25 | contract ContractA is ContractB, ContractC, ContractD {
   | ^ (Relevant source part starts here and spans across multiple lines).
Note: Missing implementation:
 --> contracts/Inheritance.sol:6:5:
  |
6 |     function whoAreYou() public virtual view returns (string memory);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The clue for the correct solution is further down: Note: Missing implementation:

Only abstract contracts can declare functions that are not implemented. To fix this, provide an override implementation for whoAreYou in ContractA:

Reveal code
function whoAreYou() public override pure returns (string memory) {
    return "I'm a person!";
}

Conclusion

In this lesson, you've learned how to implement and inherit from an abstract contract.