Dart Inheritance
Inheritance in Dart
Inheritance is one of the core concepts of Object-Oriented Programming (OOP). It allows one class (the child or subclass) to inherit the properties and behaviors (fields and methods) of another class (the parent or superclass). This helps promote code reuse and makes it easier to maintain and extend programs.
In Dart, a class can extend another class using the extends
keyword. The subclass can override or add new methods, and it can also inherit the properties and methods of the parent class.
Key Points:
- Subclass: A class that inherits from another class.
- Superclass: The class from which properties and methods are inherited.
- Method Overriding: A subclass can override methods from its superclass to provide a custom implementation.
Example of Inheritance in Dart
Let's create a Vehicle
class as a superclass and a Car
class as a subclass. The Car
class will inherit the properties and methods of Vehicle
and override some of them to provide specific functionality.
Explanation:
Class
Vehicle
(Superclass):- Has a field
brand
and methodsdrive()
anddisplayBrand()
. - The
drive()
method is a generic method, indicating that it's used for any vehicle, whiledisplayBrand()
prints the vehicle's brand.
- Has a field
Class
Car
(Subclass):- Inherits from
Vehicle
using theextends
keyword. - The constructor of
Car
calls the constructor ofVehicle
to initialize thebrand
. - The
drive()
method is overridden in theCar
class to provide a more specific implementation. - The
Car
class also introduces a new method,displayDoors()
, which is specific to theCar
class.
- Inherits from
In the
main()
function:- We create an object
myCar
of typeCar
and initialize it with a brand and the number of doors. - We access methods from both the superclass and subclass.
- We create an object
Output:
Key Concepts of Inheritance in Dart:
- Subclasses inherit fields and methods from their superclass.
- Method Overriding: A subclass can override methods of the superclass to change or extend functionality.
- Constructor Inheritance: The subclass can call the constructor of the superclass using
super()
. - Accessing Superclass Methods: The subclass can still call methods from the superclass, even if they are overridden, using
super.methodName()
. For example,super.drive()
can be used inside theCar
class if you need to call the originaldrive()
method from theVehicle
class.
Inheritance Hierarchy Example with Super and Subclasses:
You can create an inheritance hierarchy with multiple subclasses extending from the same superclass. Let’s add another subclass, Truck
, that extends Vehicle
.
Explanation:
- Truck Class: Another subclass of
Vehicle
with additional functionality. It overrides thedrive()
method and has a new methoddisplayLoadCapacity()
. - In the
main()
function, we create objects of bothCar
andTruck
classes and call the respective methods, demonstrating how both subclasses extend the functionality of theVehicle
class.
Output:
Key Takeaways:
- Inheritance allows you to reuse code from the superclass in the subclass.
- Method Overriding lets a subclass provide its own implementation of methods from the superclass.
- Dart supports single inheritance, meaning a class can only extend one class at a time.
- The
super()
keyword allows a subclass to call the constructor and methods of the superclass.
Inheritance is a powerful concept in OOP that helps in creating modular, reusable, and maintainable code. It allows you to model relationships between entities, such as "a car is a type of vehicle", enabling code that is more flexible and easier to extend.