Multilevel Inheritance in Dart : A Real Life Example
Dart is one of the beautiful programming languages of the modern era because it supports many modern features such as lambda expressions, asynchronous programming, and well-structured null safety. When it comes to code reusability, inheritance plays a vital role in programming. So, learning inheritance makes a better coder. In this tutorial, we’ll explore the use of multilevel inheritance in Dart.
💡Prerequisites for this Dart tutorial (optional):
What is multilevel inheritance in Dart?
In Dart, multilevel inheritance is a concept in which a Dart class inherits from another class, and then another class inherits from the second class, forming a chain of inheritance. This creates a hierarchy of classes, with each level inheriting characteristics from the level above it.
A Real Life Example
Real-Life Scenario:
Library
In our real-life scenario, the Library class represents a public library. A public library has a name (e.g., “City Library”).
Dart:
class Library
{
String? name;
Library(String name)
{
this.name = name;
}
void displayLibraryInfo()
{
print('Name of the library: $name');
}
}
Book
The Book class is a subclass of Library and represents a specific book within the library. Each book has a title, and it inherits the name property from the Library. For example, “Wonder” is a book in the “City Library.”
Dart:
class Book extends Library
{
String? title;
Book(String? libraryName, String title,): super(libraryName!)
{
this.title = title;
}
void displayBookInfo()
{
print('Book Title: $title');
}
}
Reader
The Reader class is a subclass of Book and represents an individual who checks out and reads a specific book from the library. Each reader has a name, and they can access information about the library and the book they are reading.
Dart:
class Reader extends Book
{
String? readerName;
Reader(String? libraryName , String? bookTitle, String readerName): super(libraryName!, bookTitle!)
{
this.readerName = readerName;
}
void displayReaderInfo() {
print('Reader :$readerName');
}
}
Make a new reader (Creating an object)
Reader reader = Reader('City Library', 'Wonder','Niki');
Let’s call methods from the multilevel inheritance hierarchy.
reader.displayLibraryInfo(); // inherited from Library
reader.displayBookInfo(); // inherited from Book
reader.displayReaderInfo(); // Specific to Reader
Dart Example : Full Source Code
Open DartPad or use the command line to compile the following program.
class Library
{
String? name;
Library(String name)
{
this.name = name;
}
void displayLibraryInfo()
{
print('Name of the library: $name');
}
}
class Book extends Library
{
String? title;
Book(String? libraryName, String title,): super(libraryName!)
{
this.title = title;
}
void displayBookInfo()
{
print('Book Title: $title');
}
}
class Reader extends Book
{
String? readerName;
Reader(String? libraryName , String? bookTitle, String readerName): super(libraryName!, bookTitle!)
{
this.readerName = readerName;
}
void displayReaderInfo() {
print('Reader :$readerName');
}
}
void main()
{
// Create an instance of the Reader class
Reader reader = Reader('City Library', 'Wonder','Niki');
//Call methods from the multilevel inheritance hierarchy
reader.displayLibraryInfo(); // inherited from Library
reader.displayBookInfo(); // inherited from Book
reader.displayReaderInfo(); // Specific to Reader
}
Output
Name of the library: City Library
Book Title: Wonder
Reader :Niki
Advantages of Multilevel Inheritance
Code Reusability
Multilevel inheritance facilitates the sharing of code as subclasses can acquire properties and methods from different tiers of the class hierarchy.
Polymorphism
Multilevel inheritance facilitates polymorphism, enabling instances from distinct classes to be handled as instances of a shared superclass.
Happy Coding!