Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

Class Inheritance

Inheritance is a paradigm in programming languages that allows objects to share traits similarly to how a human child inherits a trait from a parent. If a father has green eyes, his son might inherit that trait. If a daughter has blonde hair, one of her parents or grandparents contributed the gene, allowing her to inherit the blonde hair.

In a classical programming language, inheritance allows a family of objects to share behavior and properties between related classes of objects.

So far you’ve learned about the structure of a single class. Being a classical language, Dart supports single inheritance on a class-by-class basis. Single inheritance means that a class can directly inherit from only one class at a time.

You’ve already been using implicit inheritance. If no extended class is defined, all classes in Dart automatically extend the class Object. Dart implements inheritance when the extends keyword is placed after the class name declaration followed by a named identifier of the class which is to be inherited from.

Over the next few sections, you’re going to define the taxonomy of a fleet of flying vehicles. You’ll start with the most generic and work your way to the most specific.

Let’s create a base class of type Vehicle (Example 4.14).

EXAMPLE 4.14

class Vehicle extends Object {
  void turnOn(){
    print('--Turns On--');
  }

  void turnOff(){
      print('--Turns Off--');
    }
}

So far this looks like the classes you’ve already been working with. You’ll notice that you’ve declared two methods, turnOn() and turnoff(). These actions are a common trait of almost any vehicle, so they go into the base class. You will notice that the constructor method is left off. Dart will implicitly provide a constructor with no parameters. Let’s evolve the Vehicle class by using inheritance and by defining a new class (Example 4.15).

EXAMPLE 4.15

class Aircraft extends Vehicle {
  String name = "Aircraft";
  String fuelType;
  String propulsion;
  int maxspeed;

  void goForward() {
    print('--$name moves forward--');
  }
}

By using the keyword extends, you’re declaring that a class of Aircraft should acquire all the fields and default behavior provided in class Vehicle. You then define properties that would be common traits of all Aircraft. Let’s take a look at an implementation in Example 4.16.

EXAMPLE 4.16

 main() {
  Aircraft craft = new Aircraft(); //uses the implicit class constructor
  craft.turnOn(); //inherits from Vehicle
  craft.goForward(); //defined only in Aircraft
  craft.turnOff(); //inherits from Vehicle
}

//Output:
//--Turns On--
//--Aircraft moves forward--
//--Turns Off--

Next, let’s assume that when speaking, a person wouldn’t say, “I’m going to go for a ride in my aircraft.” A listener might infer some of the behaviors associated with that statement, but it’s still vague. An aircraft can be many things. Instead, they would refer to something more concrete, such as a blimp or a plane. You don’t have enough details in the Aircraft class in Example 4.16, so you’re going to declare Aircraft as an abstract class and build out a more concrete implementation with a few different types of aircraft.

Abstract Classes

An abstract class in Dart is a class used to share behavior among descendent classes. Abstract classes cannot be directly instantiated. You’re going to prefix the existing class declaration using the abstract keyword.

abstract class Aircraft extends Vehicle {
  ...
  ...
}

In the implementation from Example 4.16, on the line with new Aircraft(), there is now an error: 'Abstract classes cannot be created with a 'new' expression'. To fix this, let’s create some concrete implementations of Aircraft:

EXAMPLE 4.17

class Blimp extends Aircraft
{
  Blimp(int maxspeed) //explicit class constructor
  {
    this.maxspeed = maxspeed; //assigns values to superclass fields
    this.name = "Blimp"; //assigns values to superclass fields
  }
}

main() {
  Aircraft craft = new Blimp(73);
  craft.turnOn();
  craft.goForward();
  craft.turnOff();
}

//Ouput:
//--Turns On--
//--Blimp moves forward--
//--Turns Off—

Example 4.17 creates a concrete class Blimp. Class Blimp inherits from Aircraft, which inherits from the Vehicle class. Blimp has an explicit class constructor that has a parameter of maxspeed. The class Blimp has a namespace that now includes the inherited fields from each of the parent classes.

  • + Share This
  • 🔖 Save To Your Account