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:

  1. Subclass: A class that inherits from another class.
  2. Superclass: The class from which properties and methods are inherited.
  3. 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.

// Superclass (Parent Class) class Vehicle { // Field String brand; // Constructor to initialize the brand Vehicle(this.brand); // Method to describe the vehicle void drive() { print('Driving a vehicle...'); } // Method to display the vehicle's brand void displayBrand() { print('Brand: $brand'); } } // Subclass (Child Class) that inherits from Vehicle class Car extends Vehicle { // Additional field specific to Car int doors; // Constructor to initialize brand (inherited) and doors (specific to Car) Car(String brand, this.doors) : super(brand); // Overriding the drive method @override void drive() { print('Driving a car...'); } // Method specific to Car void displayDoors() { print('Number of doors: $doors'); } } void main() { // Creating an object of the Car class var myCar = Car('Toyota', 4); // Accessing inherited method from Vehicle class myCar.displayBrand(); // Output: Brand: Toyota // Accessing overridden method in Car class myCar.drive(); // Output: Driving a car... // Accessing method specific to Car class myCar.displayDoors(); // Output: Number of doors: 4 }

Explanation:

  • Class Vehicle (Superclass):

    • Has a field brand and methods drive() and displayBrand().
    • The drive() method is a generic method, indicating that it's used for any vehicle, while displayBrand() prints the vehicle's brand.
  • Class Car (Subclass):

    • Inherits from Vehicle using the extends keyword.
    • The constructor of Car calls the constructor of Vehicle to initialize the brand.
    • The drive() method is overridden in the Car class to provide a more specific implementation.
    • The Car class also introduces a new method, displayDoors(), which is specific to the Car class.
  • In the main() function:

    • We create an object myCar of type Car and initialize it with a brand and the number of doors.
    • We access methods from both the superclass and subclass.

Output:

Brand: Toyota Driving a car... Number of doors: 4

Key Concepts of Inheritance in Dart:

  1. Subclasses inherit fields and methods from their superclass.
  2. Method Overriding: A subclass can override methods of the superclass to change or extend functionality.
  3. Constructor Inheritance: The subclass can call the constructor of the superclass using super().
  4. 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 the Car class if you need to call the original drive() method from the Vehicle 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.

// Subclass Truck that inherits from Vehicle class Truck extends Vehicle { // Additional field specific to Truck double loadCapacity; // Constructor to initialize brand and loadCapacity Truck(String brand, this.loadCapacity) : super(brand); // Overriding the drive method @override void drive() { print('Driving a truck...'); } // Method specific to Truck void displayLoadCapacity() { print('Load Capacity: ${loadCapacity} tons'); } } void main() { // Creating objects of Car and Truck var myCar = Car('Honda', 4); var myTruck = Truck('Ford', 15.0); // Accessing methods from Car myCar.displayBrand(); // Output: Brand: Honda myCar.drive(); // Output: Driving a car... myCar.displayDoors(); // Output: Number of doors: 4 // Accessing methods from Truck myTruck.displayBrand(); // Output: Brand: Ford myTruck.drive(); // Output: Driving a truck... myTruck.displayLoadCapacity(); // Output: Load Capacity: 15.0 tons }

Explanation:

  • Truck Class: Another subclass of Vehicle with additional functionality. It overrides the drive() method and has a new method displayLoadCapacity().
  • In the main() function, we create objects of both Car and Truck classes and call the respective methods, demonstrating how both subclasses extend the functionality of the Vehicle class.

Output:

Brand: Honda Driving a car... Number of doors: 4 Brand: Ford Driving a truck... Load Capacity: 15.0 tons

Key Takeaways:

  1. Inheritance allows you to reuse code from the superclass in the subclass.
  2. Method Overriding lets a subclass provide its own implementation of methods from the superclass.
  3. Dart supports single inheritance, meaning a class can only extend one class at a time.
  4. 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.