Inheritance in Dart

Inheritance in Dart

The capability of a class to derive properties and characteristics from another class is called Inheritance.

·

5 min read

Inheritance is the ability of a program to create a new class from an existing class.

  • Parent Class- The class whose properties are inherited by the child class is called the Parent Class. The parent class is also known as the base class or superclass.

  • Child Class - The class that inherits properties from another class is called the child class. A child class is also known as a 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 the same for all 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 below figure:

  • You can clearly see that the above process results in the duplication of the 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 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 the vehicle class:

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

Syntax

class child_class extends parent_class {
  //body of child class
}
  • The child class inherits all the properties and methods except the constructor from the parent class.

Types of Inheritance

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 subclass is inherited by one base class only.

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.

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

Conclusion

The article discusses inheritance in Dart, a programming language. Inheritance allows a class to inherit properties and characteristics from another class, making it possible to create a new class based on an existing one.

Here are the key points from the article:

  1. Parent Class: The class whose properties are inherited is called the parent class, base class, or superclass.

  2. Child Class: The class that inherits properties from another class is called the child class, derived class, or subclass.

  3. Example: Consider the example of creating classes for vehicles like Bus, Car, and Truck. Without inheritance, you'd need to duplicate code for common functions like fuelAmount(), capacity(), and applyBrakes() in each class, leading to redundancy and potential errors.

  4. Benefits of Inheritance: Inheritance helps avoid code duplication and promotes reusability. By creating a base class (e.g., Vehicle) with common methods, you can inherit these methods in child classes (e.g., Bus, Car, Truck) and reduce redundancy.

  5. Types of Inheritance:

    • Single Inheritance: A class can inherit from only one class. Each child class can have its unique properties and methods.

    • Multiple Inheritance: Dart does not support multiple inheritance, meaning a class cannot inherit from multiple classes simultaneously.

    • Multi-level Inheritance: In this type, a derived class is created from another derived class, forming a chain of inheritance.

Overall, inheritance is a fundamental concept in object-oriented programming that promotes code reusability and organization by allowing child classes to inherit properties and methods from a parent class.

That’s it for Inheritance guys. It’s one of the most important concepts of Object Oriented Programming. Please Practice it, and have a clear understanding of it. Also Feel free to let me know if I missed something, I’d love to learn it from you. Till then Keep Loving, Keep Coding. And I’ll surely catch you up in the 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 hesitant 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.

Learn More about Dart and Flutter

Follow me for more such content.

Did you find this article valuable?

Support Jay Tillu by becoming a sponsor. Any amount is appreciated!