Abstract Classes in Dart
In this lesson, you will learn about abstract classes in Dart. Abstract classes are used to define a common structure that other classes must follow.
They are especially useful when you want to enforce certain methods while allowing each child class to implement them differently.
What Is an Abstract Class?
An abstract class is a class that:
- Cannot be instantiated directly
- May contain abstract methods (methods without a body)
- Can also contain concrete (implemented) methods
Abstract classes act as a blueprint for other classes.
Why Use Abstract Classes?
- To enforce a common structure
- To achieve abstraction
- To avoid code duplication
- To design scalable systems
Real-World Example Scenario
Consider a payment system. All payment methods must process payments, but each payment type behaves differently.
This is a perfect use case for abstract classes.
Creating an Abstract Class
abstract class Payment {
void processPayment(double amount);
void paymentInfo() {
print('Processing payment...');
}
}
Here:
processPayment()is an abstract methodpaymentInfo()is a concrete method
Implementing Abstract Class Methods
Every class that extends an abstract class must implement all abstract methods.
class CreditCardPayment extends Payment {
@override
void processPayment(double amount) {
print('Credit Card Payment of $amount processed');
}
}
Another Implementation Example
class UpiPayment extends Payment {
@override
void processPayment(double amount) {
print('UPI Payment of $amount successful');
}
}
Using Abstract Class Objects
Although you cannot create an object of an abstract class, you can use it as a reference type.
void main() {
Payment payment1 = CreditCardPayment();
payment1.paymentInfo();
payment1.processPayment(2500.50);
print('');
Payment payment2 = UpiPayment();
payment2.paymentInfo();
payment2.processPayment(1200.75);
}
This allows flexible and dynamic behavior at runtime.
Abstract vs Normal Classes
- Abstract classes cannot be instantiated
- They can contain abstract and concrete methods
- Normal classes must implement all abstract methods if extending one
Abstract Classes vs Interfaces
In Dart:
- Abstract classes can contain implemented methods
- Interfaces (using
implements) require full implementation - Abstract classes support inheritance
Common Mistakes
- Trying to create an object of an abstract class
- Forgetting to override abstract methods
- Not using
@overrideannotation
📝 Practice Exercises
Exercise 1
Create an abstract class BankAccount with a method calculateInterest().
Exercise 2
Create two child classes SavingsAccount and FixedDeposit.
Exercise 3
Calculate interest differently for each account type.
✅ Practice Answers
abstract class BankAccount {
double balance;
BankAccount(this.balance);
double calculateInterest();
}
class SavingsAccount extends BankAccount {
SavingsAccount(double balance) : super(balance);
@override
double calculateInterest() {
return balance * 0.04;
}
}
class FixedDeposit extends BankAccount {
FixedDeposit(double balance) : super(balance);
@override
double calculateInterest() {
return balance * 0.07;
}
}
void main() {
BankAccount acc1 = SavingsAccount(50000);
BankAccount acc2 = FixedDeposit(100000);
print(acc1.calculateInterest());
print(acc2.calculateInterest());
}
What’s Next?
In the next lesson, you will learn about Interfaces in Dart — how to implement multiple contracts and achieve full abstraction.