Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Abstract contracts in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Abstract contracts
Define abstract contract
↓
Declare abstract functions
↓
Cannot deploy abstract contract
↓
Create derived contract
↓
Implement all abstract functions
↓
Deploy derived contract
↓
Use derived contract in blockchain
Abstract contracts define functions without code. They must be implemented in child contracts before deployment.
Execution Sample
Blockchain / Solidity
abstract contract Animal {
    function sound() public virtual returns (string memory);
}

contract Dog is Animal {
    function sound() public override returns (string memory) {
        return "Woof!";
    }
}
Defines an abstract contract Animal with an abstract function sound, then Dog implements sound.
Execution Table
StepActionContractFunctionResult/State
1Define abstract contractAnimalsound()Declared as abstract, no implementation
2Try to deploy AnimalAnimal-Error: Cannot deploy abstract contract
3Define derived contractDog-Inherits Animal
4Implement abstract functionDogsound()Returns "Woof!"
5Deploy Dog contractDog-Success: Contract deployed
6Call sound() on DogDogsound()Returns "Woof!"
💡 Deployment stops at abstract contract; only derived with full implementation can deploy.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
Contract AnimalAbstract, no codeAbstract, no codeAbstract, no codeAbstract, no code
Contract DogNot definedDefined, inherits Animalsound() implementedDeployed and usable
Function sound()Abstract in AnimalAbstract in AnimalImplemented in DogReturns "Woof!"
Key Moments - 3 Insights
Why can't we deploy the abstract contract Animal directly?
Because Animal has an abstract function without implementation, deployment fails as shown in execution_table step 2.
What happens if Dog does not implement the abstract function sound()?
Dog would also be abstract and cannot be deployed, similar to Animal in step 2.
How do we know Dog's sound() function is used when called?
Because Dog overrides sound() with implementation, as shown in step 4 and calling it returns "Woof!" in step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 2 when trying to deploy Animal?
ADeployment succeeds with abstract functions
BDeployment fails because Animal is abstract
CDeployment succeeds but functions do nothing
DDeployment succeeds only if sound() is called
💡 Hint
Check execution_table row with Step 2 for deployment result
At which step does Dog implement the abstract function sound()?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table row describing function implementation
If Dog did not implement sound(), what would happen when deploying Dog?
ADeployment would fail like Animal
BDeployment would succeed but sound() returns empty
CDeployment would succeed normally
DDeployment would succeed but Dog is abstract
💡 Hint
Refer to key_moments about implementation necessity and execution_table step 2
Concept Snapshot
abstract contract ContractName {
    function func() public virtual returns (type); // no body
}

contract Derived is ContractName {
    function func() public override returns (type) { ... } // must implement
}

- Abstract contracts cannot be deployed
- Derived contracts must implement all abstract functions
- Only fully implemented contracts can be deployed
Full Transcript
Abstract contracts in blockchain define functions without code, called abstract functions. These contracts cannot be deployed directly because they are incomplete. To use them, you create a derived contract that inherits the abstract contract and implements all its abstract functions. Only then can the derived contract be deployed and used on the blockchain. For example, an abstract contract Animal declares an abstract function sound(). The Dog contract inherits Animal and implements sound() to return "Woof!". Trying to deploy Animal fails, but deploying Dog succeeds. Calling sound() on Dog returns "Woof!". This ensures contracts have required functions implemented before deployment.

Practice

(1/5)
1.

What is the main purpose of an abstract contract in blockchain programming?

easy
A. To define functions without implementation that child contracts must complete
B. To deploy a contract directly on the blockchain
C. To store data permanently without functions
D. To create a user interface for blockchain apps

Solution

  1. Step 1: Understand abstract contract role

    Abstract contracts declare functions without full code, acting as templates.
  2. Step 2: Identify deployment capability

    They cannot be deployed alone but require child contracts to implement missing parts.
  3. Final Answer:

    To define functions without implementation that child contracts must complete -> Option A
  4. Quick Check:

    Abstract contract = function templates [OK]
Hint: Abstract contracts declare incomplete functions for children [OK]
Common Mistakes:
  • Thinking abstract contracts can be deployed directly
  • Confusing abstract contracts with interfaces
  • Believing abstract contracts store data only
2.

Which of the following is the correct syntax to declare an abstract contract in Solidity?

?
easy
A. contract MyContract abstract { function foo() public; }
B. abstract contract MyContract { function foo() public virtual; }
C. abstract MyContract contract { function foo() public; }
D. contract MyContract { abstract function foo(); }

Solution

  1. Step 1: Check abstract contract declaration

    In Solidity, use 'abstract contract' before the contract name.
  2. Step 2: Verify function declaration

    Abstract functions are declared with 'virtual' and no body.
  3. Final Answer:

    abstract contract MyContract { function foo() public virtual; } -> Option B
  4. Quick Check:

    Correct abstract syntax = abstract contract MyContract { function foo() public virtual; } [OK]
Hint: Use 'abstract contract' and 'virtual' for abstract functions [OK]
Common Mistakes:
  • Placing 'abstract' after 'contract'
  • Missing 'virtual' keyword on abstract functions
  • Trying to define function body in abstract contract
3.

Given the following Solidity code, what will be the output when child.getValue() is called?

abstract contract Parent {
    function getValue() public virtual returns (string memory);
}

contract Child is Parent {
    function getValue() public override returns (string memory) {
        return "Hello from Child";
    }
}
medium
A. Compilation error due to missing function body in Parent
B. "Hello from Parent"
C. "Hello from Child"
D. Runtime error because Parent is abstract

Solution

  1. Step 1: Understand abstract function in Parent

    Parent declares getValue() as abstract (no body), so Child must implement it.
  2. Step 2: Check Child's implementation

    Child overrides getValue() and returns "Hello from Child" string.
  3. Final Answer:

    "Hello from Child" -> Option C
  4. Quick Check:

    Child's override runs = "Hello from Child" [OK]
Hint: Child must implement abstract functions; output from child method [OK]
Common Mistakes:
  • Expecting Parent's abstract function to run
  • Thinking abstract contracts cause runtime errors
  • Confusing compilation errors with runtime errors
4.

Identify the error in this Solidity code snippet:

abstract contract Base {
    function doSomething() public virtual;
}

contract Derived is Base {
    // Missing implementation of doSomething
}
medium
A. Derived contract must implement doSomething() or be declared abstract
B. Base contract should not have virtual functions
C. Derived contract cannot inherit from abstract contracts
D. doSomething() must have a function body in Base

Solution

  1. Step 1: Check abstract function in Base

    doSomething() is abstract (no body), so Derived must implement it or be abstract.
  2. Step 2: Analyze Derived contract

    Derived does not implement doSomething() and is not abstract, causing error.
  3. Final Answer:

    Derived contract must implement doSomething() or be declared abstract -> Option A
  4. Quick Check:

    Abstract functions require implementation in non-abstract children [OK]
Hint: Non-abstract child must implement all abstract functions [OK]
Common Mistakes:
  • Assuming virtual means implemented
  • Forgetting to mark Derived as abstract if no implementation
  • Thinking abstract contracts can be instantiated
5.

You want to create a contract system where multiple child contracts share a common function signature but implement it differently. How should you design the parent contract?

hard
A. Use an interface with implemented functions
B. Use a regular contract with fully implemented functions
C. Use a library instead of a contract
D. Use an abstract contract with the function declared as virtual and no body

Solution

  1. Step 1: Understand shared function signature need

    Multiple children must share function names but implement differently.
  2. Step 2: Choose abstract contract design

    Abstract contracts declare functions without bodies, forcing children to implement them.
  3. Step 3: Exclude other options

    Regular contracts have implementations, libraries can't enforce function signatures, interfaces can't have implementations.
  4. Final Answer:

    Use an abstract contract with the function declared as virtual and no body -> Option D
  5. Quick Check:

    Abstract contract enforces function signature for children [OK]
Hint: Abstract contracts enforce shared function signatures [OK]
Common Mistakes:
  • Using regular contracts which fix implementation
  • Confusing interfaces with implemented functions
  • Trying to use libraries for function enforcement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard