Dart Polymorphism


Polymorphism in Dart

Polymorphism is one of the key concepts of Object-Oriented Programming (OOP), and it refers to the ability of a single function, method, or operator to work in different ways depending on the type of object it is acting upon. In simpler terms, polymorphism allows objects of different classes to be treated as objects of a common superclass, and it enables overriding and method overloading.

There are two main types of polymorphism:

  1. Compile-time Polymorphism (Method Overloading) – This occurs when multiple methods have the same name but different parameters.
  2. Runtime Polymorphism (Method Overriding) – This occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.

In Dart, polymorphism is primarily achieved through method overriding (runtime polymorphism), as Dart does not support method overloading directly.


Types of Polymorphism in Dart:

  1. Method Overriding (Runtime Polymorphism): This allows a subclass to provide a specific implementation of a method that is already defined in its parent class.

  2. Subtype Polymorphism (Using Interfaces or Inheritance): This allows you to use an object of a subclass where an object of a superclass is expected.


Example of Polymorphism in Dart (Method Overriding)

Let's consider a base class Animal with a method speak(), and two subclasses Dog and Cat that override the speak() method.

// Base class class Animal { void speak() { print('Animal makes a sound'); } } // Subclass Dog class Dog extends Animal { @override void speak() { print('Dog barks'); } } // Subclass Cat class Cat extends Animal { @override void speak() { print('Cat meows'); } } void main() { // Creating objects of Dog and Cat Animal animal1 = Dog(); // Using polymorphism Animal animal2 = Cat(); // Using polymorphism // Invoking the overridden methods animal1.speak(); // Output: Dog barks animal2.speak(); // Output: Cat meows }

Explanation:

  • Class Animal: Defines a method speak() that is intended to be overridden by subclasses.
  • Class Dog and Class Cat: Both inherit from the Animal class and override the speak() method to provide their own implementation.
  • In the main() function, we create objects of Dog and Cat, but we assign them to variables of type Animal. This is where polymorphism occurs — the type of object (Dog or Cat) determines which version of the speak() method gets called.
  • The method speak() is dynamically resolved at runtime, and the correct version of the method is called based on the object type.

Output:

Dog barks Cat meows

Example of Polymorphism Using Method Overloading (Not Supported in Dart)

Dart does not support method overloading directly (i.e., defining multiple methods with the same name but different parameters). However, you can achieve similar functionality by using optional parameters or named parameters.

Here's an example using optional parameters to simulate polymorphism based on the number of arguments:

class Calculator { // Method to add two numbers (method with default parameters) int add(int a, [int b = 0]) { return a + b; } } void main() { var calc = Calculator(); print(calc.add(10)); // Output: 10 (only one parameter) print(calc.add(10, 5)); // Output: 15 (two parameters) }

Explanation:

  • The add method in the Calculator class can accept one or two arguments. The second argument b is optional, and if it's not provided, it defaults to 0.
  • In this case, the same method name add is used, but it behaves differently depending on the number of parameters passed.

Output:

10 15

Key Points of Polymorphism in Dart:

  1. Method Overriding is the most common form of polymorphism in Dart. It allows a subclass to provide a specific implementation of a method that was already defined in its superclass.
  2. Subtype Polymorphism is achieved through inheritance and interfaces. A subclass object can be treated as an object of its superclass.
  3. Dart does not directly support method overloading like some other languages, but similar behavior can be achieved using optional or named parameters.

Polymorphism is a powerful feature of OOP, as it allows you to write more flexible and reusable code, and enables you to work with objects of different types in a uniform manner.