Dart - Concept of Inheritance
Last Updated : 12 Jul, 2025
In Dart, one class can inherit another class, i.e. dart can create a new class from an existing class. We make use of extend keyword to do so.
Terminology:
- Parent Class: It is the class whose properties are inherited by the child class. It is also known as a base class or super class.
- Child Class: It is the class that inherits the properties of the other classes. It is also known as a derived class or sub class.
Example:
// parent class or base class or super class
class parent_class{
...
}
// child class or derived class or sub class
class child_class extends parent_class{
...
}
Types of Inheritance
- Single Inheritance: This inheritance occurs when a class inherits a single-parent class.
- Multi-Level Inheritance: This inheritance occurs when a class inherits from another class, which is already derived from a parent class..
- Hierarchical Inheritance: Multiple child classes inherit from a common parent class.
- Multiple Inheritance: This inheritance occurs when a class inherits more than one parent class. Dart doesn't support this.
Important Points
- Child classes inherit all properties and methods except constructors of the parent class.
- Like Java, Dart also doesn't support multiple inheritance.
1. Single Inheritance
In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes, it is also known as simple inheritance. In the below figure, ‘A’ is a parent class, and ‘B’ is a child class. Class ‘B’ inherits all the properties of class ‘A’.
Program :
Dart // Dart program to show the single inheritance // Creating parent class class Gfg { // Creating a function void output() { print("Welcome to gfg!!\nYou are inside output function."); } } // Creating Child class class GfgChild extends Gfg { // We are not defining // any thing inside it... } void main() { // Creating object of GfgChild class var geek = new GfgChild(); // Calling function // inside Gfg(Parent class) geek.output(); }
Output:
Welcome to gfg!!
You are inside output function.
2. Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting a base class, and as well as the derived class also acts as the base class for other classes. In the below image, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C.
Program :
Dart // Dart program for multilevel interitance // Creating parent class class Gfg{ // Creating a function void output1(){ print("Welcome to gfg!!\nYou are inside the output function of Gfg class."); } } // Creating Child1 class class GfgChild1 extends Gfg{ // Creating a function void output2(){ print("Welcome to gfg!!\nYou are inside the output function of GfgChild1 class."); } } // Creating Child2 class class GfgChild2 extends GfgChild1{ // We are not defining // any thing inside it... } void main() { // Creating object // of GfgChild class var geek = new GfgChild2(); // Calling function inside Gfg //(Parent class of Parent class) geek.output1(); // Calling function inside GfgChild // (Parent class) geek.output2(); }
Output:
Welcome to gfg!!
You are inside the output function of Gfg class.
Welcome to gfg!!
You are inside the output function of GfgChild1 class.
3. Hierarchical inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In the image below, class A serves as a base class for the derived classes B, C, and D.
Program :
Dart // Dart program for Hierarchical inheritance // Creating parent class class Gfg { // Creating a function void output1() { print("Welcome to gfg!!\nYou are inside output function of Gfg class."); } } // Creating Child1 class class GfgChild1 extends Gfg { // We are not defining // any thing inside it... } // Creating Child2 class class GfgChild2 extends Gfg { // We are not defining // any thing inside it... } class GfgChild3 extends Gfg { // We are not defining // any thing inside it... } void main() { // Creating object of GfgChild1 class var geek1 = new GfgChild1(); // Calling function inside Gfg(Parent class) geek1.output1(); // Creating object of GfgChild1 class var geek2 = new GfgChild2(); // Calling function inside Gfg(Parent class) geek2.output1(); // Creating object of GfgChild1 class var geek3 = new GfgChild3(); // Calling function inside Gfg(Parent class) geek3.output1(); }
Output:
Welcome to gfg!!
You are inside output function of Gfg class.
Welcome to gfg!!
You are inside output function of Gfg class.
Welcome to gfg!!
You are inside output function of Gfg class.
4. Multiple Inheritance (Not Supported in Dart)
Dart does not support multiple inheritance directly. This means a class cannot inherit from more than one parent class. However, Dart provides mixins as an alternative to achieve similar functionality.
Program:
Dart // Defining a mixin A with a method showA() mixin A { void showA() { print("This is class A."); } } // Defining a mixin B with a method showB() mixin B { void showB() { print("This is class B."); } } // Class C using mixins A and B // Instead of traditional multiple inheritance, Dart allows // using mixins to reuse code from multiple sources. class C with A, B { void showC() { print("This is class C."); } } void main() { // Creating an instance of class C C obj = C(); // Calling methods from mixins A and B, and class C obj.showA(); // Accessing method from mixin A obj.showB(); // Accessing method from mixin B obj.showC(); // Accessing method from class C }
Output :
This is class A.
This is class B.
This is class C.
Conclusion
Inheritance in Dart allows for code reuse and establishes a structured class hierarchy through the use of the extends keyword. Dart supports single inheritance, multilevel inheritance, and hierarchical inheritance; however, it does not permit multiple inheritance to reduce complexity. Instead, Dart offers mixins as a method for sharing code across multiple classes. Understanding inheritance is crucial for building scalable and maintainable applications.
Explore
Dart Tutorial
7 min read
Basics
Data Types
Control Flow
Key Functions
Object-Oriented Programming
Dart Utilities
Dart Programs
Advance Concepts
My Profile