Inheritance in dart is defined as the process in which one class derive the properties and characteristics of another class. It is helpful as it provides an ability with which we can create a new class from an existing class.
Inheritance is a major component of a programming paradigm known as OOPS (Object-Oriented Programming).
With the help of Inheritance, one class can make use of all the properties and characteristics of another class.
In general, two classes are required for inheritance and these mainly are −
Parent class - A class that is inherited by the other class is known as the parent class. Sometimes, we also refer it to as the base class.
Child class - A class that inherits the properties and characteristics of the parent class is known as child class.
Syntax
The syntax for a class to inherit the properties and characteristics of another class looks something like this −
class Childclass extends ParentClass {
…
}
Whenever a child class wants to inherit the properties and characteristics of a parent class, we make use of the extends keyword.
There are different types of inheritance that are possible in Dart. Mainly these are −
Single level inheritance
Multi-level inheritance
Hierarchal inheritance
Single Level Inheritance
In the single inheritance, a class is inherited by a single class or subclass is inherited by one parent class. In the following example, we create Person which inherits Human class.
Example
Consider the example shown below −
Live Demo
class Human{
void walk(){
print("Humans walk!");
}
}
// inherting the parent class i.e Human
class Person extends Human{
void speak(){
print("That person can speak");
}
}
void main(){
Person p = new Person();
p.speak();
p.walk(); // invoking the parent class method
}
In the above example, we have two classes, named Human and Person respectively, the class named Human is the superclass and the class named Person is the child class, which is inheriting the method named walk() from the class named Human.
Output
That person can speak
Humans walk!
MultiLevel Inheritance
Multilevel inheritance in dart is the case when different classes are inheriting in a form of chain, i.e., one class extends some parent class, and the other class extends the class that was extending the parent class.
The syntactical representation of multilevel inheritance looks something like this −
class A {}
class B extends A {}
class C extends B {}
If we notice the above syntax, we can clearly see that the class A is the parent class for the class B, which is extending it. Also, the class B acts as a parent for class C, which is extending class B.
Multilevel inheritance is nothing but a chaining of inheritance.
Example
Let's consider an example, where we make use of different classes to form a multilevel inheritance in a dart program.
Consider the example shown below −
class Wood{
void printName(){
print("Inside class Wood");
}
}
class Table extends Wood{
void printTable(){
print("Inside Table class");
}
}
class TableLegs extends Table{
void printTableLegs(){
print("Inside TableLegs class");
}
}
void main(){
TableLegs tl = new TableLegs();
tl.printTableLegs();
tl.printTable();
tl.printName();
}
In the above example, we have three different classes, namely Wood, Table, and TableLegs. Inside the main function, we create an object of the TableLegs class and then invoke the methods of the parent class that the class extends.
Output
Inside TableLegs class
Inside Table class
Inside class Wood
Hierarchical Inheritance
Hierarchical inheritance is the case of inheritance when two classes inherit a single class.
The syntactic representation of a hierarchical inheritance looks something like this −
class A {}
class B extends A {}
class C extends A {}
In the above syntactic representation, we can see that two classes, namely B and C are inheriting (or extending) class A.
Example
Let's consider an example of hierarchical inheritance in dart. Consider the example shown below −
class Parent{
void printName(){
print("Inside class Parent");
}
}
class Daughter extends Parent{
void age(age){
print("Her age is: ${age}");
}
}
class Son extends Parent{
void name(name){
print("My name is: ${name}");
}
}
void main(){
Daughter d = new Daughter();
d.printName();
d.age(23);
Son s = new Son();
s.printName();
s.name("Tuts");
}
In the above example, we have two classes, namely Son and Daughter that are extending the class named Parent.
Output
Inside class Parent
Her age is: 23
Inside class Parent
My name is: Tuts