Introduction to Inheritance

Welcome back to our exploration of Object-Oriented Programming (OOP) in Dart. In this session, we'll delve into Inheritance, a core concept that elevates code reusability and efficiency.

What is Inheritance?

Inheritance in Object-Oriented Programming (OOP) allows a new class, known as the Child class, to inherit properties and methods from another class, termed the Parent class. This principle not only facilitates code reusability but also organizes classes into a hierarchical structure.

To understand inheritance better, we're going to dive into a practical example where we use Vehicle as the parent class and Car as the child class. This will help us see how child classes can adopt attributes and behaviors from parent classes, while also introducing or modifying features to meet their specific needs. In Dart, the relationship between parent and child classes is established using the extends keyword.

Parent Class: Vehicle

We begin by defining a Vehicle class that includes common properties and methods applicable to all types of vehicles.

Child Class: Car

Next, we extend the Vehicle class to create a Car class. The Car class inherits properties from Vehicle and adds a specific characteristic, the number of wheels.

Following the demonstration of the Car class, you might have noticed two pivotal keywords in action: extends and super.

  • extends Keyword:

    • Used to establish a subclass (e.g., Car) that inherits properties and methods from a superclass (e.g., Vehicle), the extends keyword streamlines code reuse and maintenance. It signifies that Car is a specialized version of Vehicle.
  • super Keyword:

    • The super keyword in the subclass constructor calls the superclass's constructor, ensuring inherited properties (like name and speed from Vehicle) are initialized properly. This maintains the integrity of the inherited properties during object creation.

In essence, extends lets a subclass inherit from a superclass, while super initializes inherited properties correctly, facilitating effective code reuse and organization in Dart programs.

Demonstrating Inheritance

Now in the main function, let’s instantiate a Car object and observe how it utilizes both inherited methods and its specialized method.

Overriding Methods in Inheritance

In some scenarios, a child class may need to modify an inherited method to suit its specific requirements. Dart allows method overriding for this purpose, enabling a child class to provide a new implementation for an inherited method. This is where the @override annotation comes into play, serving as a clear indicator that the method below it is intended to replace an inherited method with the same signature.

Example of overriding the move method in the Car class:

To see the @override feature in action, consider the following example where we instantiate a Vehicle and a Car and invoke their move method:

In this demonstration, myVehicle.move() invokes the original method defined in the Vehicle class, whereas myCar.move() invokes the overridden method in the Car class, which includes additional information specific to cars. This ability to override methods ensures that child classes can behave in ways that are more appropriate for their context while still benefiting from methods and properties defined in their parent classes.

Lesson Summary

Today, we've dissected inheritance in Dart, by using ‘Vehicle’ and ‘Car’ classes to demonstrate inheritance in action, highlighting how child classes inherit and potentially modify properties and methods from parent classes, alonside the significance of the extends and super keywords in establishing and facilitating this inheritance.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal