Free Ebook cover How to create apps from scratch to advanced using Flutter and Dart complete course

How to create apps from scratch to advanced using Flutter and Dart complete course

5

(4)

267 pages

Advanced Dart Concepts: Inheritance and Polymorphism

Capítulo 40

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

In programming, inheritance and polymorphism are two fundamental object-oriented concepts. In Dart, these concepts are implemented very effectively and intuitively. Let's start with inheritance.

Inheritance

Inheritance is a mechanism that allows a class to inherit the properties and methods of another class. The class it inherits from is called a subclass or derived class, and the class it inherits from is called a superclass or base class.

In Dart, inheritance is implemented using the 'extends' keyword. For example, consider the following 'Animal' base class:

class Animal {
  void eat() {
    print('Eating...');
  }
}

We can create a subclass 'Dog' that inherits from 'Animal' as follows:

class Dog extends Animal {
  void bark() {
    print('Barking...');
  }
}

Now, an object of the 'Dog' class can access both the 'eat()' method of the 'Animal' base class, and the 'bark()' method of the class itself:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

var d = Dog();
d.eat(); // Output: Eating...
d.bark(); // Output: Barking...

This is inheritance. Class 'Dog' inherits all properties and methods from class 'Animal' and can also add its own methods and properties.

Polymorphism

Polymorphism is a concept that allows an object to be treated as an instance of a base class, even though it is actually an instance of a subclass. This is very useful when you want to write code that can handle objects from many different classes, as long as all those classes are subclasses of a certain base class.

In Dart, polymorphism is implemented through the use of base type variables to reference subclass objects. For example, consider the following base class 'Shape' and two subclasses 'Circle' and 'Square':

abstract class Shape {
  voiddraw();
}

class Circle extends Shape {
  void draw() {
    print('Drawing a circle...');
  }
}

class Square extends Shape {
  void draw() {
    print('Drawing a square...');
  }
}

Now, we can create a function that accepts an object of type 'Shape' and calls the 'draw()' method on it:

void drawShape(Shape shape) {
  shape.draw();
}

We can then pass a 'Circle' or 'Square' object to this function, and the appropriate 'draw()' method will be called:

var c = Circle();
drawShape(c); // Output: Drawing a circle...

var s = Square();
drawShape(s); // Output: Drawing a square...

This is polymorphism. The 'drawShape()' function can handle any object that is an instance of 'Shape' or any of its subclasses.

In summary, inheritance and polymorphism are two very powerful concepts in object-oriented programming that allow you to write more reusable and flexible code. In Dart, these concepts are implemented in a very intuitive and easy-to-use way, making Dart an excellent choice for application development.

Now answer the exercise about the content:

Which of the following concepts in object-oriented programming allows an object to be treated as an instance of a base class, even though it is actually an instance of a subclass?

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

You missed! Try again.

Polymorphism allows an object to be treated as an instance of a base class, even though it is actually an instance of a subclass. It enables writing code that works with objects from various subclasses of a base class, promoting code flexibility and reuse.

Next chapter

Advanced Dart Concepts: Mixins

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.