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

Dart Basics: Inheritance and Polymorphism

Capítulo 25

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Before diving into building apps using Flutter, it's important to have a solid understanding of the Dart programming language, which is the backbone of Flutter. Two fundamental concepts in Dart, and in object-oriented programming in general, are inheritance and polymorphism. Let's explore these concepts in detail.

Inheritance

Inheritance is a fundamental principle of object-oriented programming that allows a class to inherit the properties and methods of another class. The class being inherited is often called the 'parent class' or 'superclass', while the inheriting class is called the 'child class' or 'subclass'.

In Dart, inheritance is accomplished using the 'extends' keyword. For example, if we have a class 'Animal' with properties like 'name' and 'age' and methods like 'eat' and 'sleep', we can create a new class 'Dog' that inherits from 'Animal' as follows:< /p>

class Animal {
  String name;
  int age;

  eat() {
    print('$name is eating.');
  }

  to sleep() {
    print('$name is sleeping.');
  }
}

class Dog extends Animal {
  bark() {
    print('$name is barking.');
  }
}

The 'Dog' class now has access to all the properties and methods of the 'Animal' class, plus anything it defines by itself. This is useful because it allows us to reuse code and organize our code logically and hierarchically.

Polymorphism

Polymorphism is another fundamental principle of object-oriented programming that allows a class to have many forms. This can mean that a child class can override a parent class method, or that a method can process different types of objects.

In Dart, polymorphism is accomplished using the 'override' keyword. For example, if we want the 'Dog' class to have a different version of the 'eat' method of the 'Animal' class, we can do it as follows:

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

class Dog extends Animal {
  @override
  eat() {
    print('$name is eating dog food.');
  }
}

Now, when we call the 'eat' method on an object of the 'Dog' class, we will see the message 'name is eating dog food' instead of 'name is eating'.

Polymorphism also allows a method to process different types of objects. For example, we can have a 'makeAnimalEat' method that accepts an object of the 'Animal' class and calls its 'eat' method. This method will work both for objects of the 'Animal' class and for objects of any class that inherits from 'Animal', such as the 'Dog' class.

makeAnimalEat(Animal animal) {
  animal.eat();
}

In summary, inheritance and polymorphism are fundamental concepts in Dart that allow us to reuse and organize our code effectively. They're fundamental to building apps using Flutter, and are concepts you'll use over and over again when building your own apps.

Now answer the exercise about the content:

Which of the following object-oriented programming concepts allows a class to inherit the properties and methods of another class in Dart?

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

You missed! Try again.

The concept that allows a class to inherit the properties and methods of another class in Dart is inheritance. Inheritance is a fundamental principle of object-oriented programming that enables one class, known as the child or subclass, to inherit from another class, the parent or superclass. In Dart, the 'extends' keyword is used to establish this relationship.

Next chapter

Dart Basics: Handling Exceptions

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