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!