0
0
Blockchain / Solidityprogramming~30 mins

Abstract contracts in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Abstract Contracts in Solidity
📖 Scenario: You are building a simple blockchain-based system where different types of accounts share some common features but have their own specific behaviors. To organize your code well, you will use an abstract contract as a blueprint.
🎯 Goal: Create an abstract contract called Account with a function to get the account balance. Then, create a concrete contract called SavingsAccount that inherits from Account and implements the balance function. Finally, display the balance.
📋 What You'll Learn
Create an abstract contract named Account
Add an abstract function getBalance that returns a uint
Create a contract SavingsAccount that inherits from Account
Implement the getBalance function in SavingsAccount
Create a state variable balance in SavingsAccount initialized to 1000
Add a function to return the balance
Write a function to get the balance and print it
💡 Why This Matters
🌍 Real World
Abstract contracts help organize blockchain code by defining common interfaces for different types of accounts or contracts.
💼 Career
Understanding abstract contracts is important for blockchain developers to write clean, reusable, and maintainable smart contracts.
Progress0 / 4 steps
1
Create the abstract contract
Create an abstract contract called Account with an abstract function getBalance that returns a uint.
Blockchain / Solidity
Need a hint?

Use the abstract keyword before contract. Declare getBalance without a body and mark it virtual.

2
Create the concrete contract with balance
Create a contract called SavingsAccount that inherits from Account. Add a uint state variable called balance and set it to 1000.
Blockchain / Solidity
Need a hint?

Use is Account to inherit. Declare balance as a state variable with initial value 1000.

3
Implement the abstract function
In SavingsAccount, implement the getBalance function to return the balance variable.
Blockchain / Solidity
Need a hint?

Use override keyword and return the balance variable.

4
Display the balance
Add a public function called showBalance in SavingsAccount that returns the result of getBalance(). Then, write a line to call showBalance() and print the returned balance.
Blockchain / Solidity
Need a hint?

Create a public function that calls getBalance() and returns its value.