Dart Lesson 24 – Inheritance | Dataplexa

Inheritance in Dart

In this lesson, you will learn about inheritance in Dart. Inheritance allows one class to reuse the properties and methods of another class.

This concept helps you write reusable, organized, and scalable code, especially when working with large applications.


What Is Inheritance?

Inheritance is an Object-Oriented Programming (OOP) concept where:

  • A child class inherits from a parent class
  • The child class can reuse parent properties and methods
  • The child class can also add its own functionality

In Dart, inheritance is implemented using the extends keyword.


Basic Inheritance Example

Let’s start with a simple real-world example.

class Person {
  String name;
  int age;

  Person(this.name, this.age);

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

Here, Person is the parent class.


Creating a Child Class

Now let’s create a child class that inherits from Person.

class Employee extends Person {
  String role;
  double salary;

  Employee(String name, int age, this.role, this.salary)
      : super(name, age);

  void showEmployeeDetails() {
    displayInfo();
    print('Role: $role');
    print('Salary: $salary');
  }
}

The super keyword is used to call the parent class constructor.


Using Inherited Class Objects

Let’s create an object of the child class.

void main() {
  Employee emp = Employee('Amit', 32, 'Software Engineer', 75000);

  emp.showEmployeeDetails();
}

The Employee object can access both its own methods and methods from the Person class.


Why Use Inheritance?

Inheritance provides several advantages:

  • Reduces code duplication
  • Makes code easier to maintain
  • Improves readability
  • Supports scalability

Method Overriding

A child class can override a method from the parent class.

class Manager extends Employee {
  Manager(String name, int age, double salary)
      : super(name, age, 'Manager', salary);

  @override
  void showEmployeeDetails() {
    print('--- Manager Details ---');
    super.showEmployeeDetails();
  }
}

The @override annotation improves clarity and prevents mistakes.


Using Overridden Method

void main() {
  Manager mgr = Manager('Sonia', 40, 98000);
  mgr.showEmployeeDetails();
}

Inheritance with Real-World Analogy

Think of inheritance like this:

  • Vehicle → base class
  • Car → child class
  • ElectricCar → further specialization

Each level adds more specific behavior while reusing existing logic.


📝 Practice Exercises


Exercise 1

Create a parent class Account with properties accountHolder and balance.

Exercise 2

Create a child class SavingsAccount with an additional property interestRate.

Exercise 3

Add a method to display all account details.


✅ Practice Answers


class Account {
  String accountHolder;
  double balance;

  Account(this.accountHolder, this.balance);

  void showAccount() {
    print('Holder: $accountHolder');
    print('Balance: $balance');
  }
}

class SavingsAccount extends Account {
  double interestRate;

  SavingsAccount(String holder, double balance, this.interestRate)
      : super(holder, balance);

  void showDetails() {
    showAccount();
    print('Interest Rate: $interestRate');
  }
}

void main() {
  SavingsAccount acc =
      SavingsAccount('Ramesh', 50000, 4.5);

  acc.showDetails();
}

What’s Next?

In the next lesson, you will learn about Method Overriding in more depth — how child classes customize parent behavior safely and effectively.