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:
- Compile-time Polymorphism (Method Overloading) – This occurs when multiple methods have the same name but different parameters.
- 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:
Method Overriding (Runtime Polymorphism): This allows a subclass to provide a specific implementation of a method that is already defined in its parent class.
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.
Explanation:
- Class
Animal
: Defines a methodspeak()
that is intended to be overridden by subclasses. - Class
Dog
and ClassCat
: Both inherit from theAnimal
class and override thespeak()
method to provide their own implementation. - In the
main()
function, we create objects ofDog
andCat
, but we assign them to variables of typeAnimal
. This is where polymorphism occurs — the type of object (Dog
orCat
) determines which version of thespeak()
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:
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:
Explanation:
- The
add
method in theCalculator
class can accept one or two arguments. The second argumentb
is optional, and if it's not provided, it defaults to0
. - In this case, the same method name
add
is used, but it behaves differently depending on the number of parameters passed.
Output:
Key Points of Polymorphism in Dart:
- 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.
- Subtype Polymorphism is achieved through inheritance and interfaces. A subclass object can be treated as an object of its superclass.
- 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.