What is Inheritance?
Inheritance is a fundamental concept of object‑oriented programming that allows a class to inherit properties and methods from another class. The class that inherits is called a subclass (or child class), and the class it inherits from is called a superclass (or parent class). Inheritance promotes code reuse and establishes a hierarchical relationship between classes.
Single Inheritance in Dart
Dart supports single inheritance – a class can extend only one superclass. This keeps the inheritance hierarchy simple and avoids the complexities of multiple inheritance (like the diamond problem). However, Dart provides mixins as a way to reuse code from multiple sources.
Basic Syntax: Using extends
To create a subclass, use the extends keyword followed by the superclass name. The subclass automatically inherits all non‑private members (fields and methods) of the superclass.
The super Keyword
The super keyword refers to the superclass. It is used to call superclass constructors, methods, or access superclass properties. In the constructor, super() must be called in the initializer list.
Method Overriding
A subclass can override a method from the superclass by providing its own implementation. Use the @override annotation (optional but recommended) to indicate that you are intentionally overriding a method. The overridden method must have the same signature (return type and parameters).
Inheritance and Constructors
Constructors are not inherited, but a subclass must call a constructor of the superclass. If the superclass doesn't have a default constructor (a constructor with no parameters), you must explicitly call a specific superclass constructor using super in the initializer list.
Preventing Inheritance: final and sealed Classes
You can prevent a class from being extended by marking it final (cannot be extended) or sealed (can be extended only within the same library). This is useful for creating immutable or restricted hierarchies.
The covariant Keyword
When overriding a method, you can narrow the type of a parameter using the covariant keyword. This allows you to accept a more specific subtype than the superclass method.
Common Mistakes
- Forgetting to call
superwhen the superclass has no default constructor – leads to a compile error.
- Forgetting to call
- Accidentally overriding a method without
@override– can lead to subtle bugs if the superclass method signature changes.
- Accidentally overriding a method without
- Overriding a method and changing its parameter type – not allowed unless using
covariant.
- Overriding a method and changing its parameter type – not allowed unless using
- Trying to extend a
finalclass – compile error.
- Trying to extend a
- Confusing inheritance with composition – sometimes composition is a better design choice.
Complete Example
Key Takeaways
- Inheritance allows a class to reuse code from another class.
- Use
extendsto create a subclass.
- Use
- Dart supports single inheritance only.
- Use
superto refer to the superclass and call its constructor or methods.
- Use
- Override methods with
@overrideto provide subclass‑specific behavior.
- Override methods with
- -A subclass must call a superclass constructor (implicitly or explicitly).
- Use
finalorsealedto prevent inheritance.
- Use
- The
covariantkeyword allows narrowing parameter types in overrides.
- The