5.6. Introduction to Object Oriented Programming in Dart: Polymorphism

Página 62

Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and software programs. Dart, the programming language behind Flutter, is an object-oriented language that supports concepts like classes, objects, inheritance, and polymorphism. In this section, we'll explore the concept of polymorphism in Dart.

Polymorphism

Polymorphism is a fundamental concept in object-oriented programming. It allows objects of different types to be treated as objects of a common type. The term "polymorphism" comes from the Greek words "polys", meaning "many", and "morphē", meaning "form". Therefore, polymorphism means the ability to assume many forms.

In Dart, polymorphism is implemented using abstract classes, interfaces, and inheritance. An abstract class is a class that cannot be instantiated directly. Instead, it is used as a base class for other classes. An interface, on the other hand, is a contract that specifies what a class should do, but not how it should do it. A class that implements an interface must provide an implementation for all methods defined in the interface.

Polymorphism in Dart

In Dart, we can have polymorphism in two forms: compile-time polymorphism and runtime polymorphism. Compile-time polymorphism, also known as method overloading, occurs when you have two or more methods with the same name but different parameter lists. Dart doesn't support method overloading, so we only have runtime polymorphism in Dart.

Runtime polymorphism, also known as method substitution, occurs when a child class provides a different implementation for a method that is already provided by its parent class. In Dart, we use the keyword 'override' to indicate that we are overriding a method.

Example of Polymorphism in Dart

Consider a simple example where we have an abstract class 'Animal' with a method 'eat'. We have two classes, 'Dog' and 'Cat', which extend the 'Animal' class and provide their own implementations for the 'eat' method.

abstract class Animal {
  void eat();
}

class Dog extends Animal {
  @override
  void eat() {
    print('The dog is eating');
  }
}

class Cat extends Animal {
  @override
  void eat() {
    print('The cat is eating');
  }
}

Now, we can create 'Dog' and 'Cat' objects and treat them as 'Animal'. This is polymorphism. We can call the 'eat' method on an 'Animal' object, and the correct implementation of the method will be called based on the actual type of the object.

void main() {
  Animal dog = Dog();
  Animal cat = Cat();

  dog.eat(); // prints 'The dog is eating'
  cat.eat(); // prints 'The cat is eating'
}

In short, polymorphism allows us to treat objects of different types uniformly. It increases the flexibility and modularity of our code, making it more reusable and easier to maintain.

Now answer the exercise about the content:

What is polymorphism in object-oriented programming and how is it implemented in Dart?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

635.7. Introduction to object-oriented programming in Dart: Interfaces

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text