Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and methods from another class. This promotes code reusability and establishes a natural hierarchy between classes.

Key Concepts

  1. Base Class (Parent Class): The class whose properties and methods are inherited.
  2. Derived Class (Child Class): The class that inherits from the base class.
  3. extends Keyword: Used to indicate that a class is inheriting from another class.

Why Use Inheritance?

  • Code Reusability: Avoids redundancy by reusing existing code.
  • Hierarchical Classification: Establishes a relationship between base and derived classes.
  • Maintainability: Easier to manage and update code.

Syntax

To create a derived class that inherits from a base class, use the extends keyword.

class Animal {
  void eat() {
    print('Animal is eating');
  }
}

class Dog extends Animal {
  void bark() {
    print('Dog is barking');
  }
}

void main() {
  Dog dog = Dog();
  dog.eat();  // Inherited method
  dog.bark(); // Method of Dog class
}

Practical Example

Let's create a more detailed example involving a base class Vehicle and a derived class Car.

Base Class: Vehicle

class Vehicle {
  String brand;
  int year;

  Vehicle(this.brand, this.year);

  void displayInfo() {
    print('Brand: $brand, Year: $year');
  }
}

Derived Class: Car

class Car extends Vehicle {
  String model;

  Car(String brand, int year, this.model) : super(brand, year);

  void displayCarInfo() {
    displayInfo(); // Calling the method from the base class
    print('Model: $model');
  }
}

Main Function

void main() {
  Car car = Car('Toyota', 2020, 'Corolla');
  car.displayCarInfo();
}

Explanation

  • Base Class (Vehicle): Contains properties brand and year, and a method displayInfo().
  • Derived Class (Car): Inherits properties and methods from Vehicle and adds a new property model and a method displayCarInfo().
  • Constructor: The derived class constructor calls the base class constructor using super.

Exercises

Exercise 1: Create a Base and Derived Class

  1. Create a base class Person with properties name and age, and a method displayPersonInfo().
  2. Create a derived class Student that inherits from Person and adds a property studentId.
  3. Add a method displayStudentInfo() in the Student class that calls displayPersonInfo() and also prints the studentId.

Solution

class Person {
  String name;
  int age;

  Person(this.name, this.age);

  void displayPersonInfo() {
    print('Name: $name, Age: $age');
  }
}

class Student extends Person {
  String studentId;

  Student(String name, int age, this.studentId) : super(name, age);

  void displayStudentInfo() {
    displayPersonInfo(); // Calling the method from the base class
    print('Student ID: $studentId');
  }
}

void main() {
  Student student = Student('Alice', 20, 'S12345');
  student.displayStudentInfo();
}

Exercise 2: Override a Method

  1. Create a base class Shape with a method area() that returns 0.
  2. Create a derived class Rectangle that overrides the area() method to return the area of the rectangle.

Solution

class Shape {
  double area() {
    return 0;
  }
}

class Rectangle extends Shape {
  double width;
  double height;

  Rectangle(this.width, this.height);

  @override
  double area() {
    return width * height;
  }
}

void main() {
  Rectangle rectangle = Rectangle(5, 10);
  print('Area of Rectangle: ${rectangle.area()}');
}

Common Mistakes and Tips

  • Forgetting to Call super: When a derived class has a constructor, ensure to call the base class constructor using super.
  • Overriding Methods: Use the @override annotation to indicate that a method is being overridden. This helps in catching errors during compilation.
  • Accessing Base Class Members: Use super to access methods and properties of the base class if they are overridden in the derived class.

Conclusion

Inheritance is a powerful feature in Dart that allows you to create a hierarchy of classes, promoting code reuse and maintainability. By understanding and utilizing inheritance, you can write more efficient and organized code. In the next section, we will explore Mixins, another way to reuse code in Dart.

© Copyright 2024. All rights reserved