Start a Project

Inheritance in Dart

Inheritance in Dart

Inheritance in Dart is a way, where a class (child) acquires the properties and behaviors of another class (parent). This mechanism allows the subclass to reuse code and extend the functionality of the parent class, which in turn creates a hierarchical structure.

In Dart, the extends keyword is used for inheritance between classes. When a class extends another class, it inherits all the non-private members (i.e., fields and methods) of the superclass. The subclass can then override or add new functionalities to these inherited members.

Let’s start with a simple example to illustrate inheritance in Dart:

In this example, we have a superclass Animal with a single field name and a method makeSound() and haveTail(). Then, we define a subclass Dog that extends the Animal class. The Dog class inherits the name property and haveTail() method from the Animal class.

Method Overriding

The basic benefit of inheritance is the ability to override methods in the subclass. If a subclass has a method name same as the one in its parent class but has a different implementation and use cases according to the needs of the subclass. This process is known as method overriding.

Here, we have another subclass Cat that extends the Animal class The Cat class overrides the makeSound() method and writes its own code/implementation.

You can refer to the article on method overriding here.

super Keyword

In Dart, the ‘super’ keyword allows a subclass to refer to its parent class and access its methods and properties. It is useful when we want to call the parent class’s implementation of a method before modifying the subclass functionality.

In this example, we have a Puppy class that extends Dog. The makeSound() method in Puppy calls super.makeSound() to execute the makeSound() method of the Dog class before printing an additional message.

You can also refer to the article on ‘super’ keyword here.

Multi-Level Inheritance in Dart

Dart supports multi-level inheritance, which means a subclass can be a parent class for another class. However, it’s essential to avoid deep inheritance hierarchies to prevent code complexities.

In the example above the Puppy class is a subclass of the Dog class and the Dog class is itself inherited from the Animal class, this is known as Multi-level inheritance in Dart.

Conclusion

Inheritance is a powerful feature in Dart that enables the creation of hierarchical relationships between classes, promoting code reusability and structured programming.

Dart’s support for inheritance makes it an excellent choice for building object-oriented applications, providing developers with the tools to create robust and scalable codebases.

Inheritance is one of the basic principles of Object-oriented programming. You can check out the other principles of OOP in my blogs:

Abstraction in Dart
Polymorphism in Dart

You may also check our Flutter app development page.

Happy coding!

Exit mobile version