5.5. Introduction to Object Oriented Programming in Dart: Inheritance
Page 61 | Listen in audio
Object-oriented programming (OOP) is a programming paradigm that uses "objects" - data structures consisting of data fields and methods together with their interactions - to design applications and computer programs. In Dart, as in many other object-oriented languages, inheritance plays a crucial role in the organization and structure of code. In this section, we'll explore Dart inheritance and how it can be used when building apps with Flutter.
Inheritance in Dart
Inheritance is a fundamental principle of object-oriented programming that allows one class to inherit the fields and methods of another. In Dart, inheritance is implemented through the 'extends' keyword. When a class is declared to inherit from another class, it becomes a subclass and the class it inherits from becomes the superclass.
class Animal { void breathe() { print('Breathing...'); } } class Dog extends Animal { void bark() { print('Barking...'); } }
In the above example, the Dog class inherits from the Animal class. This means that a Dog object can perform both the 'bark' action and the 'breathe' action.
Method overrides
In Dart, a subclass can override a method of its superclass using the 'override' keyword. This allows the subclass to provide a different implementation of a method that is already provided by its superclass.
class Animal { void breathe() { print('Breathing...'); } } class Dog extends Animal { void bark() { print('Barking...'); } @override void breathe() { super.breathe(); print('...with a bone in the mouth'); } }
In the example above, the Dog class overrides the 'breathe' method of the Animal class. Calling the 'super.breathe()' method invokes the original implementation of the 'breathe' method in the superclass Animal, and then the Dog class adds its own functionality to it.
Inheritance and Flutter
When building apps with Flutter, inheritance can be used to reuse and extend the behavior and appearance of widgets. For example, you can subclass a stateful widget to add additional functionality or to customize its appearance.
class CustomButton extends RaisedButton { CustomButton({@required VoidCallback onPressed}) : super( onPressed: onPressed, color: Colors.blue, child: Text( 'Custom Button', style: TextStyle(color: Colors.white), )); }
In the above example, the 'CustomButton' class inherits from 'RaisedButton' and customizes its color and text. Inheritance allows you to create custom widgets that fit seamlessly into your application without having to rewrite all of your widget code.
Conclusion
Inheritance is a powerful feature of object-oriented programming that allows developers to reuse and extend existing code. In Dart and Flutter, it's an essential tool for organizing code and creating custom widgets. Understanding inheritance and how to use it effectively can help improve efficiency and code quality when building apps with Flutter.
Now answer the exercise about the content:
What is the role of inheritance in object-oriented programming in Dart and Flutter?
You are right! Congratulations, now go to the next page
You missed! Try again.
Next page of the Free Ebook: