![Inheritance in Dart](https://mobikul.com/wp-content/uploads/2023/08/featuredImageForInheritanceInDart-1024x576.png)
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Parent class class Animal { String name; Animal(this.name); void makeSound() { print('make sound'); } void haveTail() { print('any length of the tail'); } } // subclass class Dog extends Animal { Dog(String name) : super(name); @override void haveTail() { print('wag tail'); } } |
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.
1 2 3 4 5 6 7 8 |
class Cat extends Animal { Cat(String name) : super(name); @override void makeSound() { print('Meow!'); } } |
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.
1 2 3 4 5 6 7 8 9 |
class Puppy extends Dog { Puppy(String name) : super(name); @override void makeSound() { super.makeSound(); print('any implementation'); } } |
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!