In this track: Write and read Solidity
advanced · 4 MIN READ

Interfaces and inheritance basics

Shared declarations and inherited implementation have different jobs.

LET’S PICTURE IT

A plug specification says how to connect; inheriting a component can bring its actual machinery too.

SEE IT FOR YOURSELF
A valid counter call, step by stepStep 1 / 4
A valid counter call, step by stepFunction requestincrement()No argumentsBefore executionstored count0A local teaching example with a defined starting stateIncoming callCheck function dispatchCheck coded conditionsRules passConditions passContinue executionA failed check can revert insteadCalculation0 + 1Result: 1Storage writecount = 1Persists if call succeedsCalculating a local value and storing it are separate stepsPersistent statecount1Later readGetter reports 1No new incrementState, logs and browser display have different roles

This example begins with count zero and a request to call increment(). No real contract is deployed.

Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.

An interface describes callable shapes

Solidity interfaces declare expected external behavior without providing the ordinary implementation body for those functions. They help code call a contract through an agreed type.

Inheritance can bring code and state

A derived contract can inherit behavior and variables from bases. The effective permissions and storage layout can therefore depend on declarations outside the visible derived file.

Overrides need inspection

Language rules determine how overrides are declared and selected. A familiar base name does not prove that a derived override preserves every safety property.

The deployed address still matters

Casting an address to an interface does not prove matching code is deployed there. It tells the compiler how to encode calls, leaving runtime behavior and network identity as separate evidence.

Read the example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface ICounter {
    function count() external view returns (uint256);
}

contract LearningExample {
    function read(ICounter target) external view returns (uint256) {
        return target.count();
    }
}

The target address must actually provide compatible behavior for the call to succeed.

QUICK CHECK · NO PRESSURE

Does casting an address to an interface authenticate the code at that address?

Your progress stays in this browser.
Keep exploring the original referenceSolidity documentation ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.