Dart: Inheritance

Dart: Inheritance

Dart inheritance is defined as the process of deriving the properties and characteristics of another class. It provides the ability 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 base class or superclass.

  • Child Class - The class that inherits properties from another class is called the child class. The child class is also known as the 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 Jinali extends Person {}

main(){
  var jinali = new Jinali();

  jinali.showName("JG");
  jinali.showAge(21);
}

Output
JG
21
  • 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 Jinali extends Person {
  void showProfession(String profession) {
    print(profession);
  }

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

main() {
  var reet = new Reet();

  sanket.showName("Reet");
  sanket.showAge(14);
  sanket.showNationality("Indian");
  sanket.showProfession("Student");
}

Output

Reet
14
Indian
Student

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.