Dart – 10 – Inheritance

Inheritance in Dart Programming

Inheritance is a core concept in object-oriented programming (OOP) that allows you to create new classes based on existing ones. In Dart, an expressive and versatile programming language, inheritance plays a crucial role in code reusability and the modeling of relationships between classes. In this discussion, we’ll explore the significance of inheritance in Dart, understand how it works, and see practical examples of its use.

Understanding Inheritance

Inheritance is the process by which a new class, referred to as a subclass or derived class, inherits the properties and methods of an existing class, known as the superclass or base class. The subclass can extend and specialize the behavior of the superclass while inheriting its common characteristics.

Creating Subclasses in Dart

In Dart, you create a subclass by defining a new class that extends an existing class using the extends keyword. This new class inherits all the members (properties and methods) of the superclass and can add its own members or override inherited members.

Here’s a simple example of a superclass and a subclass in Dart:


class Shape {
    void draw() {
        print('Drawing a shape');
    }
}

class Circle extends Shape {
    @override
    void draw() {
        print('Drawing a circle');
    }
}
    
Overriding Methods

When a subclass extends a superclass, it can provide its own implementation of methods defined in the superclass. This process is called method overriding. The @override annotation is used to indicate that a method in the subclass is intended to override a method in the superclass.

In the example above, the Circle class overrides the draw method to provide a specialized implementation. When you create a Circle object and call draw, it prints ‘Drawing a circle’ instead of ‘Drawing a shape’.

Accessing Superclass Members

In Dart, you can access members of the superclass within the subclass by using the super keyword. This allows you to call methods or access properties from the superclass when necessary.

Here’s an example demonstrating the use of super to access the draw method of the superclass within the Circle class:


class Circle extends Shape {
    @override
    void draw() {
        super.draw(); // Calls the draw method of the superclass
        print('Drawing a circle');
    }
}
    
Abstract Classes and Interfaces

Dart allows you to define abstract classes and use them as a basis for other classes. An abstract class cannot be instantiated but can be extended by other classes. It serves as a blueprint for common characteristics and methods that subclasses must implement.

Additionally, Dart supports interfaces, which define a contract of methods that implementing classes must adhere to. Interfaces allow you to define a common set of methods and properties that multiple classes can implement without enforcing a specific inheritance hierarchy.

Conclusion

Inheritance is a fundamental concept in Dart that enables code reusability and the modeling of relationships between classes. Subclasses can inherit and extend the behavior of superclasses, providing a powerful mechanism for building complex and organized code structures. By understanding and leveraging inheritance, you can design more flexible and efficient applications in Dart.