0
0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style9 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.