Sitemap
Dart Tutorials

Dart Tutorials for beginners and advanced programmers

Polymorphism in Dart

2 min readMar 27, 2025

--

Polymorphism is a fundamental concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. In Dart, polymorphism is primarily achieved through method overriding and interfaces.

Press enter or click to view image in full size

Read Medium Story for free 👇

Method Overriding in Polymorphism

Polymorphism allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This is done using the @override annotation.

class Animal {
void makeSound() {
print("Animal makes a sound");
}
}

class Dog extends Animal {
@override
void makeSound() {
print("Dog barks");
}
}

class Cat extends Animal {
@override
void makeSound() {
print("Cat meows");
}
}

void main() {
Animal myAnimal = Dog();
myAnimal.makeSound(); // Output: Dog barks

myAnimal = Cat();
myAnimal.makeSound(); // Output: Cat meows
}

Using Abstract Classes for Polymorphism

--

--

Usama Sarwar
Usama Sarwar

Written by Usama Sarwar

Helping founders turn ideas into market-ready web and mobile apps within 30 days using Google’s Flutter & Next.js without juggling multiple teams.

Dart Tutorials
Dart Tutorials

Published in Dart Tutorials

Dart Tutorials for beginners and advanced programmers