4.2. Advanced Dart Concepts: Inheritance and Polymorphism

Página 40

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:

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.

Next page of the Free Ebook:

414.3. Advanced Dart Concepts: Mixins

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