How to Implement Inheritance In Dart?

10 minutes read

Inheritance is an important concept in object-oriented programming that allows a class to inherit properties and methods from another class. In Dart, you can implement inheritance using the extends keyword.


To create an inheritance relationship between two classes, you need to define a base class (also known as a superclass) and a derived class (also known as a subclass). The derived class inherits all the properties and methods of the base class.


Here's an example code snippet to help you understand how to implement inheritance in Dart:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Animal {
  String name;
  
  Animal(this.name);
  
  void eat() {
    print('$name is eating.');
  }
  
  void sleep() {
    print('$name is sleeping.');
  }
}

class Dog extends Animal {
  Dog(String name) : super(name);
  
  void bark() {
    print('$name is barking.');
  }
}

void main() {
  var dog = Dog('Buddy');
  
  print(dog.name);  // Output: Buddy
  dog.eat();        // Output: Buddy is eating.
  dog.sleep();      // Output: Buddy is sleeping.
  dog.bark();       // Output: Buddy is barking.
}


In the example, we have defined two classes - Animal and Dog. The Animal class serves as the base class with properties like name and methods like eat() and sleep(). The Dog class is derived from the Animal class using the extends keyword and adds its own method called bark(). To call the superclass's constructor, we use super(name) in the derived class's constructor.


In the main() function, we create an instance of the Dog class and demonstrate that it has access to all the properties and methods of the Animal class through inheritance.

Best Dart Books to Read in 2024

1
Flutter and Dart Cookbook: Developing Full-Stack Applications for the Cloud

Rating is 5 out of 5

Flutter and Dart Cookbook: Developing Full-Stack Applications for the Cloud

2
Flutter Cookbook: Over 100 proven techniques and solutions for app development with Flutter 2.2 and Dart

Rating is 4.9 out of 5

Flutter Cookbook: Over 100 proven techniques and solutions for app development with Flutter 2.2 and Dart

3
Quick Start Guide to Dart Programming: Create High-Performance Applications for the Web and Mobile

Rating is 4.8 out of 5

Quick Start Guide to Dart Programming: Create High-Performance Applications for the Web and Mobile

4
Dart: Up and Running: A New, Tool-Friendly Language for Structured Web Apps

Rating is 4.7 out of 5

Dart: Up and Running: A New, Tool-Friendly Language for Structured Web Apps

5
The Dart Programming Language

Rating is 4.6 out of 5

The Dart Programming Language

6
Mastering Dart: A Comprehensive Guide to Learn Dart Programming

Rating is 4.5 out of 5

Mastering Dart: A Comprehensive Guide to Learn Dart Programming

7
Flutter Cookbook: 100+ step-by-step recipes for building cross-platform, professional-grade apps with Flutter 3.10.x and Dart 3.x, 2nd Edition

Rating is 4.4 out of 5

Flutter Cookbook: 100+ step-by-step recipes for building cross-platform, professional-grade apps with Flutter 3.10.x and Dart 3.x, 2nd Edition

8
Flutter for Beginners: An introductory guide to building cross-platform mobile applications with Flutter 2.5 and Dart, 2nd Edition

Rating is 4.3 out of 5

Flutter for Beginners: An introductory guide to building cross-platform mobile applications with Flutter 2.5 and Dart, 2nd Edition


How to check if an object is an instance of a particular class in Dart inheritance?

To check if an object is an instance of a particular class in Dart inheritance, you can use the is keyword followed by the class name.


Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Animal {
  // ...
}

class Dog extends Animal {
  // ...
}

void main() {
  var dog = Dog();

  // Check if 'dog' is an instance of 'Animal'
  if (dog is Animal) {
    print('dog is an instance of Animal class');
  }

  // Check if 'dog' is an instance of 'Dog'
  if (dog is Dog) {
    print('dog is an instance of Dog class');
  }
}


In the above example, the dog object is first checked against the Animal class using the is keyword. If it is an instance of the Animal class, the first if condition will be true and the corresponding message will be printed. Similarly, the dog object is then checked against the Dog class, and if it is an instance of the Dog class, the second if condition will be true and the message will be printed.


How to create a class that inherits from another class in Dart?

To create a class that inherits from another class in Dart, you can use the extends keyword followed by the name of the superclass. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Animal {
  void sound() {
    print('Animal makes sound');
  }
}

class Dog extends Animal {
  @override
  void sound() {
    print('Dog barks');
  }
}

void main() {
  Dog dog = Dog();
  dog.sound();  // Output: Dog barks
}


In the above example, the Dog class inherits from the Animal class using the extends keyword. The Dog class then overrides the sound() method of the superclass to provide its own implementation.


By using the @override annotation before the sound() method in the Dog class, it explicitly indicates that the method is intended to override the same method in the superclass. This is not mandatory in Dart, but it helps to prevent accidental method name typos or incorrect signatures.


When you create an instance of the Dog class and invoke the sound() method, it will call the overridden implementation in the Dog class, which prints "Dog barks" to the console.


What is inheritance in Dart?

Inheritance in Dart is a way to create new classes based on existing classes. It allows a class to inherit properties and methods from another class, known as the parent class or superclass.


A child class, also known as a subclass, can inherit properties and methods from the parent class and can also add its own additional properties and methods. This promotes code reusability and allows for the creation of more specialized classes.


To establish an inheritance relationship between classes in Dart, the keyword extends is used. By extending a class, the child class inherits all of the properties and methods of the parent class. The child class can then override inherited methods or add new methods and properties.


Here's an example of inheritance in Dart:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Animal {
  String name;
  
  void eat() {
    print('$name is eating.');
  }
}

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

void main() {
  Dog dog = Dog();
  dog.name = 'Buddy';
  dog.eat();
  dog.bark();
}


In the above code, the Dog class extends the Animal class using the extends keyword. As a result, the Dog class inherits the name property and eat() method from the Animal class. Then, a new method bark() is added to the Dog class.


The main() function demonstrates the usage of the classes. The dog object is created as an instance of the Dog class, and it can access both the inherited method eat() and the new method bark().


Output:

1
2
Buddy is eating.
Buddy is barking.


In this way, inheritance provides a way to build hierarchies of classes, enabling code reuse and creating more specialized classes by deriving from existing ones.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To send emails via Gmail from Dart, you can use the mailer package. Here are the steps to achieve this:Set up your Dart project by creating a new Dart file and importing the necessary packages. import 'package:mailer/mailer.dart'; import 'package:m...
Dart DevTools is a powerful tool for debugging Dart applications. It provides a comprehensive set of features to help you understand and analyze the behavior of your code during runtime. Here's an overview of how to use Dart DevTools for debugging:Installa...
To read user input in Dart, you can use the standard input/output methods provided by the dart:io library. Here's how you can do it:Import the dart:io library: import 'dart:io'; Create an instance of the stdin object from the io library: var input ...