Inheritance in Dart
Tags: dart, todayilearned, beginners
Published: 2019-09-16 05:38:56

The capability of a class to derive properties and characteristics from another class is called **Inheritance**. It is ability of a program to create new class from an existing class.

* **Parent Class -** The class whose properties are inherited by child class is called Parent Class. Parent class is also known as ***base class*** or ***super class***.

* **Child Class -** The class that inherits properties from another class is called child class. Child class is also known as ***derived class*** or ***base class***.

* Consider a group of vehicles. You need to create classes for Bus, Car and Truck. The methods fuelAmount(), capacity(), applyBrakes() will be same for all of the three classes. If we create these classes avoiding inheritance then we have to write all of these functions in each of the three classes as shown in below figure:

![](https://miro.medium.com/max/879/1*qoCOtaoHHRutXCupS3TruA.png)

* You can clearly see that above process results in duplication of same code 3 times. This increases the chances of error and data redundancy. To avoid this type of situation, inheritance is used.
    
* If we create a class Vehicle and write these three functions in it and inherit the rest of the classes from the vehicle class, then we can simply avoid the duplication of data and increase re-usability. Look at the below diagram in which the three classes are inherited from vehicle class:

![](https://miro.medium.com/max/942/1*sZAbHf60I7tzz0Az7wTJ6Q.png)

* Using inheritance, we have to write the functions only one time instead of three times as we have inherited rest of the three classes from base class(Vehicle).

####Syntax
```dart
class child_class extends parent_class {
  //body of child class
}
```

* Child class inherits all the properties and methods except constructor from parent class.

##Types of Inheritance
***

Mainly there are three types of inheritance:

1. Single
2. Multiple - Dart doesn’t support Multiple Inheritance.
3. Multi-level

###Single Level Inheritance

* In single inheritance, a class is allowed to inherit from only one class. i.e. one sub class is inherited by one base class only.

![](https://miro.medium.com/max/314/1*QU6OtMgKoFG5dHKpkfuaVg.png)

```dart
class Person{
  void showName(String name){
    print(name);
  }

  void showAge(int age){
    print(age);
  }
}

class Jay extends Person {}

main(){
  var jay = new Jay();

  jay.showName("JD");
  jay.showAge(20);
}

Output
JD
20
```

* Here also remember that child class can have its own unique properties and methods too.

###Multi-Level Inheritance

* In this type of inheritance, a derived class is created from another derived class.

![](https://miro.medium.com/max/484/1*7Zijjh4nJl6_6G6RWaCffA.png)

```dart
class Person {
  void showName(String name) {
    print(name);
  }

  void showAge(int age) {
    print(age);
  }
}

class Jay extends Person {
  void showProfession(String profession) {
    print(profession);
  }

  void showNationality(String nationality) {
    print(nationality);
  }
}
//Derived class created from another derived class.
class Sanket extends Jay {} 

main() {
  var sanket = new Sanket();

  sanket.showName("Sanket");
  sanket.showAge(20);
  sanket.showNationality("Indian");
  sanket.showProfession("Engineer");
}

Output

Sanket
20
Indian
Engineer
```

Guys credit for this article goes to Harsh Agrawal from geeksforgeeks.com. He wrote an amazing article on Inheritance in C++. Here I just try to explain same thing in dart way. 😉

That’s it for Inheritance guys. It’s one of the most important concept of Object Oriented Programming. Please Practice it, and have clear understanding on it. Also Feel free to let me know if I misses something, I’ll love to learn it from you. Till then Keep Loving, Keep Coding. And I’ll surely catch you up in next article.


Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitate that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

###For More Information Please Visit Following Links
* [Fuchsia OS official site](https://fuchsia.dev/)
* [Dart official site](https://dart.dev/)
* [Flutter official site](https://flutter.dev/)


Jai Hind, Vande Mataram 🇮🇳

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊

- [My Site] (https://www.jaytillu.in)
- [My Blogs] (https://blogs.jaytillu.in)
- [Twitter](https://twitter.com/jay_tillu)
- [Facebook](https://www.facebook.com/jaytillu.1314/)
- [Instagram](https://www.facebook.com/jaytillu.1314/)
- [Medium](https://medium.com/jay-tillu)
- [LinkedIn](https://www.linkedin.com/in/jaytillu/)
- [Github](https://www.github.com/jay-tillu)
- [Stack overflow](https://stackoverflow.com/users/8509590/jay-tillu)
or just mail me at jd.tillu@gmail.com