Inheritance in Dart
Let's forget programming for a moment and talk about real life. What do we understand when someones says the word inheritance?
Inheritance is the process of passing on properties from one generation to another. For example, a child inherits the properties of his/her parents and grandparents which continues this from generation to generation.
Before we dive into inheritance in Dart, let's first understand the keyword extends, this and super in Dart.
Keyword | Description |
---|---|
extends | The keyword extends is used to inherit properties from a parent class. |
this | The keyword this is used to refer to the current instance of the class. |
super | The keyword super is used to refer to the parent class. |
This keywords are very important to understand and implement inheritance in Dart.
What is Inheritance in Dart Programming?
In programming, inheritance is the process of passing on properties from one class to another.
The class that passes on the properties is called the parent class or the superclass. The class that inherits the properties is called the child class or the subclass.
Inheritance is a very important concept in object oriented programming. It allows us to reuse code and also to add more features to a class without modifying it.
Example
Let's look at an example of inheritance in Dart.
class Animal {
String name;
int age;
Animal(this.name, this.age);
void eat() {
print('$name is eating');
}
void sleep() {
print('$name is sleeping');
}
}
class Dog extends Animal {
Dog(String name, int age) : super(name, age);
void bark() {
print('$name is barking');
}
}
void main() {
var dog = Dog('Bruno', 2);
dog.eat();
dog.sleep();
dog.bark();
}
Output
Bruno is eating
Bruno is sleeping
Bruno is barking
Types of Inheritance
There are different types of inheritance in Dart. Dart supports the following types of inheritance.
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
Single Inheritance in Dart
Single inheritance is the process of inheriting properties from a single parent class. This means, If there are class A and class B, and class B inherits from class A, then it is called single inheritance.
Example
class Animal {
String name;
int age;
Animal(this.name, this.age);
void eat() {
print('$name is eating');
}
void sleep() {
print('$name is sleeping');
}
}
class Dog extends Animal {
Dog(String name, int age) : super(name, age);
void bark() {
print('$name is barking');
}
}
void main() {
var dog = Dog('Bruno', 2);
dog.eat();
dog.sleep();
dog.bark();
}
This is an example of single inheritance. The class Dog
inherits from the class Animal
. Let's take the output of code.
Output
Bruno is eating
Bruno is sleeping
Bruno is barking
Multi-level Inheritance in Dart
Multi-level inheritance is the process of inheriting properties from a parent class and then inheriting properties from the child class.
This means, If there are class A, class B and class C, and class B inherits from class A and class C inherits from class B, then it is called multi-level inheritance.
Example
class Animal {
String name;
int age;
Animal(this.name, this.age);
void eat() {
print('$name is eating');
}
void sleep() {
print('$name is sleeping');
}
}
class Dog extends Animal {
Dog(String name, int age) : super(name, age);
void bark() {
print('$name is barking');
}
}
class Human extends Animal {
Human(String name, int age) : super(name, age);
void speak() {
print('$name is speaking');
}
}
void main() {
var dog = Dog('Bruno', 2);
dog.eat();
dog.sleep();
dog.bark();
var human = Human('John', 25);
human.eat();
human.sleep();
human.speak();
}
This is an example of multi-level inheritance. The class Dog
inherits from the class Animal
and the class Human
inherits from the class Animal
. Let's take the output of code.
Output
Bruno is eating
Bruno is sleeping
Bruno is barking
John is eating
John is sleeping
John is speaking
Multilevel Inheritance in Dart
Multilevel inheritance is the process of inheriting properties from a parent class and then inheriting properties from the child class.
This means, If there are class A, class B and class C, and class B inherits from class A and class C inherits from class B, then it is called multi-level inheritance.
Example
class Animal {
String name;
int age;
Animal(this.name, this.age);
void eat() {
print('$name is eating');
}
void sleep() {
print('$name is sleeping');
}
}
class Dog extends Animal {
Dog(String name, int age) : super(name, age);
void bark() {
print('$name is barking');
}
}
class Breed extends Dog {
String breed;
Breed(String name, int age, this.breed) : super(name, age);
void displayBreed() {
print('$name is a $breed');
}
}
void main() {
var dog = Breed('Bruno', 2, 'Labrador');
dog.eat();
dog.sleep();
dog.bark();
dog.displayBreed();
}
Output
Bruno is eating
Bruno is sleeping
Bruno is barking
Bruno is a Labrador
Overriding Methods in Dart
Overriding methods is the process of redefining a method of a parent class in the child class.
This means that if there is a method in the parent class and the child class has the same method, then the method of the child class will be called while inheritance of any parent classes.
Example
class Animal {
String name;
int age;
Animal(this.name, this.age);
void eat() {
print('$name is eating');
}
void sleep() {
print('$name is sleeping');
}
}
class Dog extends Animal {
Dog(String name, int age) : super(name, age);
void bark() {
print('$name is barking');
}
@override
void sleep() {
print('$name is sleeping');
}
}
void main() {
var dog = Dog('Bruno', 2);
dog.eat();
dog.sleep();
dog.bark();
}
Output
Bruno is eating
Bruno is sleeping
Bruno is barking
Overriding Properties in Dart
Overriding properties is the process of redefining a property of a parent class in the child class.
This means that if there is a property in the parent class and the child class has the same property, then the property of the child class will be called while inheritance of any parent classes.
Example
class Animal {
final String name;
final String age ;
Animal(this.name, this.age);
}
class Dog extends Animal {
final String name;
Dog(this.name, String age) : super("$name-1", age);
}
void main() {
var dog = Dog('Bruno', 2);
print(dog.name);
}
Output
Bruno
You can notice that the output that came is Bruno
and not Bruno-1
. This is because we have overridden the property name
in the child class.