Dart Abstract Classes and Polymorphism in Dart — Part 6
In Dart, abstract classes serve as blueprints for other classes, defining common behaviors and properties that subclasses can inherit and implement. This enables polymorphic behavior, allowing different objects to respond to the same message in different ways based on their types. Let’s delve into the code examples provided to understand abstract classes and runtime polymorphism in Dart.
1) Abstract Classes
Abstract classes cannot be instantiated directly; they provide a structure for derived classes to follow. Let’s examine the abstract class Car
and its concrete implementations:
abstract class Car {
String name;
Car(this.name);
void drive();
}
The
Car
class defines an abstract methoddrive()
that must be implemented by its subclasses.It also declares a
name
property that subclasses will use to identify the car.
Concrete Implementations
Concrete classes ElectricCar
and FuelCar
extend the Car
abstract class and provide specific implementations for the drive()
method:
class ElectricCar extends Car {
double chargeCapacity;
ElectricCar(String name, this.chargeCapacity) : super(name);
@override
void drive() {
print("Driving an Electric Car");
}
}
class FuelCar extends Car {
double fuelCapacity;
FuelCar(String name, this.fuelCapacity) : super(name);
@override
void drive() {
print("Driving a Fuel Car");
}
}
ElectricCar
andFuelCar
inherit thename
property from theCar
class and implement thedrive()
method with specific behaviors for electric and fuel cars.
2) Runtime Polymorphism
Runtime polymorphism allows objects to exhibit different behaviors based on their types. In the provided code, we utilize runtime polymorphism to demonstrate how different car objects respond to the drive()
method:
void main() {
final electricCars = ElectricCar('TATA', 2900);
final fuelCars = FuelCar("BMW", 7879);
final gasCars = GasCar("Maruti", 7879);
final List<Car> carList = [electricCars, fuelCars, gasCars];
for (final Car car in carList) {
checkDrive(car);
}
}
void checkDrive(Car car) {
car.drive();
}
abstract class Car {
String name;
Car(this.name);
void drive();
}
class ElectricCar extends Car {
double chargeCapacity;
ElectricCar(String name, this.chargeCapacity) : super(name);
@override
void drive() {
print("Driving an Electric Car");
}
}
class FuelCar extends Car {
double fuelCapacity;
FuelCar(String name, this.fuelCapacity) : super(name);
@override
void drive() {
print("Driving an fuel car");
}
}
class GasCar extends Car {
double gasCapacity;
GasCar(String name, this.gasCapacity) : super(name);
@override
void drive() {
print("Driving an Gas car");
}
}
The checkDrive
function takes a Car
object as a parameter and invokes its drive()
method, which will vary depending on the type of car object passed.
Conclusion
Abstract classes and runtime polymorphism are powerful concepts in Dart that enable code reuse, modularity, and extensibility. Dart programmers can write flexible and maintainable code that adapts to different requirements and scenarios by defining common behaviors in abstract classes and leveraging polymorphism. Understanding these concepts is essential for building robust and scalable Dart applications.
Thank you for following along with this series on Dart. Keep exploring.
Certainly! Here’s the list of the Dart series, including parts 1 through 6:
Part 1: Dart Basic
Part 2: Dart Control Flow Statement
Part 6: Dart Abstract Classes and Polymorphism in Dart
These parts cover various aspects of Dart and its implementation. Each part contributes to a comprehensive understanding of Dart concepts.
🌟 Stay Connected! 🌟
Hey there, awesome reader! 👋 Want to stay updated with my latest insights,Follow me on social media!
Subscribe to my newsletter
Read articles from Sadanand gadwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Sadanand gadwal
Sadanand gadwal
I am a passionate Full Stack Web Developer specializing in the MERN stack (MongoDB, Express.js, React.js, and Node.js). I have completed my master's in MCA from the Central University of Karnataka, Kalaburagi. With a strong focus on web development, I possess expertise in technologies such as React.js, tailwind CSS, Material UI, etc.