Table of contents
1.
Introduction
2.
Syntax and Terminology used in Inheritance
3.
Types of Inheritance
3.1.
Single Inheritance
3.2.
Multiple Inheritance
3.3.
Multi-Level Inheritance
3.4.
Hierarchical Inheritance
4.
Implementation of Inheritance
5.
Frequently Asked Questions
5.1.
What all is inherited when a child class extends a parent class?
5.2.
Which inheritance is supported by Java but not by Dart?
5.3.
What are some alias names for the Parent class in Dart?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dart Inheritance

Author Vasu Bansal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will study Dart Inheritance which is one of the widely used concepts in Dart. Inheritance is used to transfer properties and methods from a parent class to a child class. It is similar to inheritance in real-world applications. You can also read the blog Dart Classes and Objects on the Coding Ninjas Website to learn more about classes and objects in Dart.

Syntax and Terminology used in Inheritance

The following is the syntax to use inheritance in Dart. The keyword extends is used to inherit the parent class to the child class.

class Parent{
  // Parent class code
}


class Child extends Parent{
// Child class code
}

When talking about inheritance, two terms are used quite frequently. They are namely, the Parent class and the Child class. The parent class and child class are quite intuitive to their real-world counterparts. Similar to how parent genes are passed to their children/offspring, the methods and properties of a Dart parent class are transferred to the child Dart class when using the extends keyword.

Types of Inheritance

There are different types of inheritance present in Dart, which are discussed as follows.

Single Inheritance

We call an inheritance type single inheritance when each child class inherits only a single parent class. For example, a Male or Female Class inherits a Human class, a Cat or Dog class inherits the Animal class and there are many other examples.

Multiple Inheritance

When a single child class inherits from multiple parent classes, it is called multiple inheritance. Unfortunately, the Dart programming language does not support this type of inheritance. For example, an Engineer Class would inherit from both Person Class and Employee class.

Multi-Level Inheritance

When the number of levels of inheritance increases more than two, it is said to have a multiple-level inheritance. For example, when class C inherits class B and class B inherits class A, then the number of inheritance levels becomes three and represents multiple-level inheritance.

Hierarchical Inheritance

When a class acts as a parent class for two or more child classes, it is said to have a hierarchical inheritance. For example, Science class is a parent class for Biology, Physics and Chemistry Classes. So this can be considered an example of Hierarchical Inheritance.

Implementation of Inheritance

In this section, we will see how is the concept of inheritance actually implemented in a Dart program. 

Code:

// Dart program to demonstrate the concept of inheritance

// Parent class
class Student{

    // Print function of the Student class
    void student_print_function(){
        print("Inside Student class print function");
    }

}

// Child class
class Ninja extends Student{

    // Print function of the Ninja class
    void ninja_print_function(){
        print("Inside Ninja class print function");
    }

}

void main(){
    var ninja_obj = new Ninja();
    ninja_obj.student_print_function(); // Calling the function of the parent class
    // Note that since the Ninja class extends the Student class, the functions of the parent class Student are accessible in the child class Ninja
    ninja_obj.ninja_print_function();
}

Output:

Inside Student class print function
Inside Ninja class print function

 

Know about Single Inheritance in Java in detail.

Frequently Asked Questions

What all is inherited when a child class extends a parent class?

Except for the parent class's constructor, the child class in Dart inherits everything from the parent class.

 

Which inheritance is supported by Java but not by Dart?

The multiple inheritance concept is supported by Java but is not present in Dart.

 

What are some alias names for the Parent class in Dart?

The parent class is also known by the names Base class or Superclass.

Conclusion

In this article, we have extensively discussed Inheritance in Dart and saw a sample program having its implementation. Feel free to refer to the blog Dart const and Final keyword on the Coding Ninjas Website to enhance your knowledge of Dart programming.
We hope this blog has helped you enhance your knowledge regarding Dart Inheritance. If you want to learn more, check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development. Do upvote our blog to help other ninjas grow.
Happy Coding!

Live masterclass