Yetesfa Alemayehu
2 min readApr 13, 2022

--

Dart Inheritance

Dart inheritance is defined as the process of deriving the properties and characteristics of another class. It provides the ability to create a new class from an existing class. It is the most essential concept of the oops(Object-Oriented programming approach). We can reuse all the behavior and characteristics of the previous class in the new class.

The newly created classes are called the child/sub classes.A class inherits from another class using the ‘extends’ keyword. Child classes inherit all properties and methods except constructors from the parent class.

  • Parent Class — The class whose properties are inherited by child class is called Parent Class. Parent class is also known as base class or super class.
  • Child Class — A class which inherits properties from other classes is called the child class. It is also known as the derived class or subclass.

Suppose we have a fleet of cars, and we create three classes as Duster, Maruti, and Jaguar. The methods modelName(), milage(), and man_year() will be the same for all of the three classes. By using the inheritance, we don’t need to write these functions in each of the three classes.

class child_class extends parent_class {

//body of child class

}

Types of Inheritance:

There are four types of Inheritance are:

  • Single Inheritance: In this inheritance when a class inherits a single parent class then this inheritance happens.
  • Multiple Inheritance: In this inheritance when a class inherits more than one parent class then this inheritance happens.

Note: Dart doesn’t support Multiple Inheritance.

  • Multi-Level Inheritance: In this inheritance when a class inherits another child class then this inheritance happens.
  • Hierarchical Inheritance: In this inheritance, more than one class has the same parent class.

class MyWidget extends StatelessWidget {

@override

Widget build(BuildContext context) {

return Container();

}

}

This is the most straightforward illustration of a custom widget in Flutter. It utilizes the extends keyword to demonstrate that the class ought to inherit properties and strategies from StatelessWidget, which itself inherits from the Widget class. This is significant because each Flutter widget has a build() technique accessible that profits an occurrence of Widget.

Everything in Dart extends Object, so boundaries composed as Object will acknowledge any value. Inheritance is at the actual center of the language. Container extends StatelessWidget which expands Widget, so build()can return a Container object, and containers can be remembered for List assortments composed as Widget.

--

--