In the Dart language, classes can be derived from other classes, thereby inheriting fields and methods from those classes. A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
In simple words, inheritance means – one can use anything of anyone. In real life example, Just like you can use anything of your parents.
Syntax –
class base{ variables; methods; } class child extends base{ variables; methods; } |
Note
To do the Inheritance, we need to use of ‘extends’ keyword. The ‘extends’ keyword will be use for only child class. Say, class B inherited by class A, means class B able to use variables and methods(functions) of class A. Example – class A{ variable1, method1 ; } class B extends A{ variable1 , method1, method2; } |
There are multiple inheritance are there in Dart
- Single Inheritance
- Multiple Inheritances
- Multi-level Inheritance
- Hybrid Inheritance
Single Inheritance
Single inheritance enables a derived class to inherit properties and behavior from a single parent class. It allows a derived class to inherit the properties and behavior of a base class, thus enabling code re-usability as well as adding new features to the existing code.

Let’s understand through below code…
Code 1:
class A{ void lang(){ print(‘language Dart Class A’); } } class B extends A{ } void main(){ // class ‘B’ object ‘b1’ var b1=B(); b1.lang(); } |

code 2:
class animal{ // parent class ‘animal’ void list(){ print(‘there are many animals like cat,dog,tiger etc.’); } } class cat extends animal{ // child class ‘cat’ void legs(){ print(‘cat has 4 legs’); } } void main(){ var pussy=cat(); // accessing parent class ‘animal’ member method -> list() pussy.list(); pussy.legs(); } |

Multiple Inheritances
Multiple Inheritances is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when there exist methods with the same signature in both the superclass’s and subclass.

Let’s understand through below code…
Code 3:
class A{ void msg(){ print(‘this is class A’); } } class B extends A{ void msg1(){ print(‘this is class B’); } } class C extends A{ void msg2(){ print(‘this is class C’); } } class D extends B{ void msg3(){ print(‘this is class D extends by B’); } } void main(){ var obj=D(); obj.msg(); obj.msg3(); } |

Multi-level Inheritance
It involves inheriting a class, which already inherited some other class. Correlating it with a real-life scenario, we’ve often seen some of our habits and thoughts match precisely with our parents. And similarly, their habits match with their parents, i.e., our grandparents. We can create hierarchies in Dart with as many layers of Inheritance as we want. This means we can utilize a subclass as a superclass. In this case, each subclass inherits all of the traits shared by all of its superclass’s.

class A{ void func(){ print(‘parent class A’); } } class B extends A{ void dis(){ print(‘child class B derived from class A’); } } class C extends B{ void myfunc(){ print(‘child class C parent B & grand parent A’); } } void main(){ var obj=C(); /* class C object ‘obj’ now it can access member variable and functions of it’s parents B,A */ obj.func(); obj.dis(); obj.myfunc(); } |

Hybrid Inheritance
A hybrid inheritance is a combination of more than one types of inheritance.

code 5:
class A{ void msg(){ print(‘this is class A’); } } class AB extends A{ void msg2(){ print(‘this is class AB extends parent class A’); } } class ACP extends AB{ void msg3(){ print(‘child class ACP extends parent class AB,grant parent A’); } } class AC extends A{ void msg4(){ print(‘child class AC extends parent class A’); } } class kali extends AC{ void msg5(){ print(‘child class kali parent AC, grant parent A’); } } class arch extends AC{ void msg6(){ print(‘child class arch parent AC, grand parent A’); } } class nmap extends kali{ void msg7(){ print(‘child class nmap, parent class kali,grant parent AC,A’); } } class johnripper extends kali{ void msg8(){ print(‘child class johnripper, parent class kali,grand-parent AC,A’); } } void main(){ var john=johnripper(); john.msg8(); john.msg5(); john.msg(); var acp=ACP(); acp.msg3(); acp.msg2(); } |
