Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
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.
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.
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.
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.
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 DartPolymorphism in Dart
You may also check our Flutter app development page.
Happy coding!
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Name
Email
Subject
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.