DEV Community

Moyeen Haider
Moyeen Haider

Posted on

Polymorphism in Dart

What is Polymorphism?

Imagine you have a magical remote control. This remote control can interact with different types of devices, like a TV, a fan, or even a robot toy. Now, polymorphism is like pressing the same button on your remote, but each device responding in its unique way.

In computer programming, polymorphism allows one thing, like a function or method, to work with different types of objects. It’s like using that magical button on your remote, and each device (object) knows how to respond to it based on its own capabilities.

Why is Polymorphism Important?

Polymorphism is important because it adds flexibility. You can use the same button (function) for various devices (objects), making your code more versatile and easier to manage.

Imagine: Think of polymorphism as having a universal remote control that works with different devices. Pressing the same button does different things for each device, showcasing their unique abilities, just like polymorphism allows one function to work with different types of objects.

Let’s try to Visualize the below code:

/**
 * Polymorphism Example in Real Life
 * 
 * Imagine you own a garage with various types of vehicles.
 * Polymorphism is like starting each vehicle in your garage using a common ignition method.
 * Each vehicle, whether a car, bike, or truck, responds uniquely to the ignition.
 * 
 * In computer programming, polymorphism allows a common method (ignition) to work with different types of objects (vehicles).
 * It provides a way for each object to respond to the method based on its specific characteristics.
 * 
 * Why is Polymorphism Important?
 * Polymorphism adds versatility to your garage. You can use the same ignition method for various vehicles,
 * making it easier to manage and operate different types of vehicles in your collection.
 * 
 * Imagine: Think of polymorphism as having a universal ignition key that works with different vehicles.
 * Turning the key starts each vehicle, and each vehicle responds uniquely based on its design,
 * just like polymorphism allows one method to work with different types of objects.
 */

// Define a class representing a Vehicle
class Vehicle {
  // Common method for starting the vehicle
  void start() {
    print('Vehicle is starting...');
  }
}

// Define a class representing a Car inheriting from Vehicle
class Car extends Vehicle {
  // Unique behavior for starting a car
  @override
  void start() {
    print('Car engine is roaring to life!');
  }
}

// Define a class representing a Bike inheriting from Vehicle
class Bike extends Vehicle {
  // Unique behavior for starting a bike
  @override
  void start() {
    print('Bike engine is revving up!');
  }
}

// Define a class representing a Truck inheriting from Vehicle
class Truck extends Vehicle {
  // Unique behavior for starting a truck
  @override
  void start() {
    print('Truck engine is rumbling!');
  }
}

/**
 * Main function where the program execution starts
 * Imagine this as the garage owner using a universal ignition method to start different vehicles in the garage.
 */
void main() {
  // Create instances of different vehicles
  Vehicle genericVehicle = Vehicle();
  Car myCar = Car();
  Bike myBike = Bike();
  Truck myTruck = Truck();

  // Start each vehicle using the common start method
  genericVehicle.start();
  myCar.start();
  myBike.start();
  myTruck.start();
}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
nigel447 profile image
nigel447

polymorphism => inheritance can lead to bad code if not careful