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:

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.

Article image Dart Basics: Handling Exceptions 26

Next page of the Free Ebook:

Dart Basics: Handling Exceptions

Estimated reading time: 3 minutes

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

+ 9 million
students

Free and Valid
Certificate

60 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video and ebooks