thumbnail

Inheritance in Dart using the Extends Keyword

Published on 29 March 2020
Last Updated on 29 March 2020

Inheritance is possible in Dart by using extends and implements keywords. We have already discussed how to implement inheritance like abstraction using implements keyword in a different chapter.

In this chapter we are going to discuss how to implement single and multi-level inheritance using the extends keyword in Dart.

Extends keyword in Dart

extends keyword can be used for creating a superclass and subclass relationship between two different classes.

Use extends keyword subclass to inherit all instance members of a superclass.

Super keyword in Dart

super keyword in Dart is used in a subclass to refer to instance members of a superclass.

Types of inheritance in Dart

Following are the different types of inheritance in Dart

  • Single inheritance.
  • Multiple inheritance.
  • Multi Level inheritance.

In this chapter, we are going to discuss what types of inheritance are possible in Dart and how we can achieve them using various examples.

Single inheritance

In this type of inheritance, there is only one superclass and subclass. Refer to program given below to understand single inheritance.

Program to demonstrate Single inheritance

Given below is a simple program that demonstrates the use of extends and super keyword in a simple way.

// program to single inheritance via extends and super keyword
class ParentClass {
  int a = 1;

  void sayHello() {
    print('Hello from ParentClass!');
  }
}

class ChildClass extends ParentClass {
  int b = 2;

  void add() {
    print('a+b is: ${a + b}');
    print('super.a+b is: ${super.a + b}');
  }

  @override
  void sayHello() {
    super.sayHello();
    print('Hello from ChildClass!');
  }
}

void main() {
  var child = new ChildClass();
  child.sayHello();
  child.add();
}

Above program produces following output:

Hello from ParentClass!
Hello from ChildClass!
a+b is: 3
super.a+b is: 3

Multiple inheritance

Dart does not fully support multiple inheritance. However, you can achieve an abstraction that is similar to multiple inheritance using interfaces.

For understanding how to use interfaces for implementing multiple inheritance, please refer to our interfaces chapter.

Multi level inheritance

Dart supports multi level inheritance. In multi-level inheritance a derived class is created from another derived class.

class A {
  var a = 1;
}

class B extends A {
  var b = 2;
}

class C extends B {
  var c = 4;
}

class D extends C {}

void main() {
  // creating a new instance of Class D
  var dInstance = new D();
  // accessing all the inherited fields
  print('''
dInstance.a == ${dInstance.a}
dInstance.b == ${dInstance.b}
dInstance.c == ${dInstance.c}
''');
}

Above program produces following output:

dInstance.a == 1
dInstance.b == 2
dInstance.c == 4

As you can see, child class D has access to all instance variables of superclass C,B and A. This is possible because of multi-level inheritance.