Dart Inheritance
Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of another class can have its own additional features as well. In Dart, one class can inherit the properties of another class; this means that a new class can be created from an existing class. This property overcomes the problem of code redundancy.
To inherit a class, we use ‘extends’ keyword.
There are some important terms associated with inheritance :
Parent Class : It is the main class, also known as the base class from which other classes derive some properties. It is sometimes also referred to as the superclass.
Child Class : It is the derived class, also known as the subclass that derives the properties from the parent class and also keeps its own features.
Here is the syntax of inheritance :
class parent_class { // body of the class } class child_class extends parent_class { // body of the class }
Consider the following code in Dart that explains the concept of inheritance:
Example 1 : Single inheritance in Dart
// Explaining the concept of inheritance using single inheritance // This is the definition of the parent class ' teacher ' class teacher { // function member output( ) void output( ) { print( " All classes have a teacher ! " ) ; } } /* This is the definition of the base class ' student ' that inherits * the base class ' teacher ' */ class student extends teacher { // function member output( ) void display( ) { print( " Every student is taught by some teacher ! " ) ; } } void main( ) { /* creating an object of the derived class ' student '. This object * can access the mmebers of both the classes ' student ' and ' teacher ' * because ' student ' class inherits ' teacher ' class. */ var s1 = new student( ) ; // accessing member function output( ) of ' teacher ' class s1.output( ) ; // accessing member function display( ) of ' student ' class s1.display( ) ; }
Output :
All classes have a teacher ! Every student is taught by some teacher !
Types of Inheritance :
There are different types of Inheritance in Dart :
- Single Inheritance : In this type of inheritance, a child class inherits only a single parent class.
- Multiple Inheritance : In this type of inheritance, a child class inherits more than one parent class. However, Dart doesn’t support this.
- Multi-Level Inheritance : In this type of inheritance, a child class inherits another child class that inherits another base class.
- Hierarchical Inheritance : In this type of inheritance, more than one child classes have the same parent class.
Important Points:
Child classes inherit all the properties and methods except constructors of the parent class.
Like Java, Dart also doesn’t support multiple inheritance.
Consider the following code in Dart that explains the concept of multilevel inheritance:
Example 2:
// Dart program to explain the concept of multilevel inheritance // This is the definition of the parent class 'teacher' class teacher { // function member output1( ) void output1( ) { print( ' This is the function of class teacher. All classes have a teacher ! ' ) ; } } /* This is the definition of the child class ' student ' that * inherits the parent class ' teacher ' */ class student extends teacher { // function member output2( ) void output2( ) { print( ' This is the function of class student. Every student has a teacher ! ' ) ; } } /* This is the definition of the child class ' classes ' that * inherits the parent class ' student ' */ class classes extends student { // function member output3( ) void output3( ) { print( ' This is the function of class classes. Every class has some students and teacher ! ' ) ; } } void main( ) { /* creating an object of the derived class ' classes '. This object * can access the members of all the classes ' student ', ' teacher ' * and ' classes ' because ' classes ' class inherits ' teacher ' and ' student ' class. */ var c1 = new classes( ) ; // accessing member function output( ) of ' teacher ' class c1.output1( ) ; // accessing member function display( ) of ' student ' class c1.output2( ) ; // accessing member function display( ) of ' classes ' class c1.output3( ) ; }
Output :
This is the function of class teacher. All classes have a teacher ! This is the function of class student. Every student has a teacher ! This is the function of class classes. Every class has some students and teacher !
Since, here the class ‘classes’ inherit the class ‘student’ which inherits another class ‘teacher’. This is an example of multilevel inheritance.
Consider the following code in Dart that explains the concept of hierarchical inheritance
Example 3 :
// Dart program to explain the concept of hierarchical inheritance // This is the definition of the parent class ' teacher ' class teacher { // member function output1( ) void output1( ) { print( ' This is the function of class teacher. All classes have a teacher ! ' ) ; } } /* This is the definition of the child class ' student ' that * inherits the parent class ' teacher ' */ class student extends teacher { // function member output2( ) void output2( ) { print( ' This is the function of class student. Every student has a teacher ! ' ) ; } } /* This is the definition of the child class ' classes ' that * inherits the parent class ' student ' */ class classes extends teacher { // function member output3( ) void output3( ) { print( ' This is the function of class classes. Every class has some students and teacher ! ' ) ; } } void main( ) { /* creating the object of the class ' student ' that inherits * the class ' teacher ' */ var s1 = new student( ) ; // calling the function output1( ) of class ' teacher ' s1.output1( ) ; // calling the function output1( ) of class ' student ' s1.output2( ) ; /* creating the object of the class ' classes ' that inherits * the class ' teacher ' */ var s2 = new classes( ) ; // calling the function output1( ) of class ' teacher ' s2.output1( ) ; // calling the function output3( ) of class ' classes ' s2.output3( ) ; }
Output :
This is the function of class teacher. All classes have a teacher ! This is the function of class student. Every student has a teacher ! This is the function of class teacher. All classes have a teacher ! This is the function of class classes. Every class has some students and teacher !
If we try to call function output2( ) of class ‘student’ using object s2, the compiler throws an error because the class ‘classes’ inherit the class ‘teacher’ and not ‘student’. Since, here two classes ‘student’ and ‘classes’ have a single parent child ‘teacher’, it is hierarchical inheritance.