Polymorphism in Dart

Polymorphism In Dart

Polymorphism is the concept of oops where one thing has various possible forms. It is the concept of the same thing having different shapes, sizes and features that distinguish it from other. It reduces the time and effort to write a repetitive code as well as flexibility to override.

Polymorphism By Method Overriding

Syntax

class ParentClass{
void functionName(){
}
}
class ChildClass extends ParentClass{
@override 
void functionName(){
}
}

Example

class Animal {
  void sound() {
    print("Animal produces sound.");
  }
}

class Cat extends Animal {}

class Dog extends Animal {
  @override
  void sound() {
    print("Dogs produces woof sound.");
  }
}

void main(List<String> args) {
  var dog = Dog();
  dog.sound();
  var cat = Cat();
  cat.sound();
}
Show Output
Info

Note: Dart doesn’t support method overloading so, each method name must be unique no matter what its parameters requirement due to support of dynamic data type.