We are already familiar with the term Polymorphism, which means having multiple forms. Polymorphism in Dart is also the same as in other languages.
There are two types of polymorphism that are Function Overloading(Compile-time) and Function Overriding(Run-time)
Dart is a dynamically typed language and Function overloading requires static types, so Function Overloading is not supported in Dart.
Now let’s explore more on polymorphism.
You may also check our Flutter app development page.
Runtime Polymorphism in Dart
Runtime polymorphism works at the run time as the name suggests. In this form of polymorphism, the method from the superclass can be overridden to perform a new type of work according to the requirements.
Let’s explain this with some examples.
Let’s create a parent class with some method performing a certain task.
1 2 3 4 5 6 |
class VehicleClass { rideVehicle(){ print("Ride bicycle"); } } |
In the above code, VehicleClass has a method rideVehicle() which performs bicycle riding. In other words, “Ride bicycle” is printed.
Now, the VehicleClass is inherited by two other classes BikeClass and CarClass like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class VehicleClass // parent class { rideVehicle(){ print("Ride bicycle"); } } class <em>BikeClass</em> extends <em>VehicleClass</em>{ } class <em>CarClass</em> extends <em>VehicleClass</em>{ } void main(){ // main method VehicleClass bike = new <em>BikeClass</em>(); VehicleClass car = new <em>CarClass</em>(); bike.rideVehicle(); car.rideVehicle(); } //OUTPUT // "Ride bicycle" //"Ride bicycle" |
We created an instance of the BikeClass as a bike and an instance of CarClass as the car in our main() method.
Above all, BikeClass and CarClass are inherited from VehicleClass so they can execute the methods defined in the VehicleClass.
After that, we called the method rideVehicle() for both bike and car instances. Output is shown as “Ride bicycle” from both instances.
Above all, if the bike instance does not want to ride a bicycle and wants to ride a bike, then simply the BikeClass can override the rideVehicle() method of the parent class in its scope according to its requirement(here riding a bike instead of bicycle). The updated code will be like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class VehicleClass // parent class { rideVehicle(){ print("Ride bicycle"); } } class <em>BikeClass</em> extends <em>VehicleClass</em>{ rideVehicle(){ print("Ride bike"); } } class <em>CarClass</em> extends <em>VehicleClass</em>{ } void main(){ // main method VehicleClass bike = new <em>BikeClass</em>(); VehicleClass car = new <em>CarClass</em>(); bike.rideVehicle(); car.rideVehicle(); } //OUTPUT // "Ride bike" //"Ride bicycle" |
This is known as method overriding or run-time polymorphism in Dart.
Advantages of Polymorphism
- Code Reusability: You can use the code written in the parent class in your child class and also modify it according to your requirements.
- Polymorphism enables us to communicate with a variety of distinct classes while hiding implementation details.
Conclusion
I hope I was able to provide the basic knowledge of polymorphism in Dart.
If you want to Explore more then visit here.
If you want to read my other articles please click here.