Add Your Heading Text Here

Dart supports Inheritance which is one of the core concept of the object oriented programming language.

Inheritance mechanism is used to consume properties and behaviour of a super class in subclass.

Inheritance is the concept by which one class can derive properties and methods of another class.

The class which derives or inherits the properties from another class is known as child class or Sub Class.

The class whose properties are derived or inherited by other classes is known as parent class or super class.

In Inheritance, child class has access to the properties and methods of its own class as well as its parent class.

In dart, Object class is the Super class of all classes. Hence, each class is derived from at least one class or have at lease one parent class except Object class.

In Dart, extends keyword is used for Inheritance.

There are 2 types of inheritance in dart.

  1. Single Inheritance
  2. Multilevel Inheritance

Single Inheritance

when properties and methods of one class is derived from exact one other class is known as Single Inheritance.

For ex :

class A{
	displayA(){
		print("A");
	}
}
class B extends A{
	displayB(){
		print("B");
	}
}

void main(){
	A a=new A();
	B b=new B();
	a.displayA();
	b.displayA();
	b.displayB();
}

Output :

A
A
B

In Above example, class B has all the properties and methods of its parent class A as well itself.

Multilevel Inheritance

Multilevel Inheritance is the type of inheritance in which a class can be derived from another derived class. i.e., A class will act as the Super class for any particular class and that same class will act as the child class for another class.

For ex :

class A{
  displayA(){
    print("A");
  }
}
class B extends A{
  displayB(){
    print("B");
  }
}
class C extends B{
  displayC(){
  }
}
void main(){
  A a=new A();
  B b=new B();
  C c=new C();
  a.displayA();
  b.displayA();
  b.displayB();
  c.displayA();
  c.displayB();
  c.displayC();
}

Output :

A
A
B
A
B

In above example, class B is derived from class A. So, class B acts as the child or sub class for class A.

Similarly, class C is derived from class B. That’s why class B acts as the parent class or super class for class C.

Also, one point to note down is that the child class will have access to the properties of its immidiate parent class as well as all its non immidiate parent classes.

Output :

class C have access to all the properties and functions of its immidiate parent class B as well as its non immidiate super parent class A.

We also need to understand the code reusability as it is the main purpose of the Inheritance

Code Reusability

It is nothing but accessing or using the properties and methods of existing class while creating a new class as per user requirement.

This concept is used to remove redundant or duplicate code written in the program for optimizing your coding skills and program structure.

Code Reusability can be easily achieved using the inheritance.

For ex :

class Favourite{
  int rank=5;
  Test(){
    print("This is Test Method");
  }
	Calculation(int a,int b){
		print(a+b);
	}
}
class Normal extends Favourite{
  
}
void main(){
	Normal normal=new Normal();
  	normal.Test();
 	print(normal.rank);
  	normal.Calculation(5,10);
}

Output :

This is Test Method
5
15

In above example, class Normal doesn’t have any properties or methods, but it can access all the properties and methods of its super class using Inheritance.

let say, if we define Calculation() function in class Favourite and further we also need Calculation() function in Normal class. So, if Normal class inherits the Favourite class then using Inheritance rules we do not need to define that function again in Normal class.

As all the properties and functions of parent class are also the properties and functions of its child class. Just as you’re parents’ property is yours too.

So, for enhancing the code structure and reusability of your code the Inheritance concept is widely used.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons