Abstract Classes in Dart
Abstract classes in Dart are those classes that only contain abstract methods (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We use the ' abstract ' keyword to declare the abstract class or method.
But, always remember that it's not necessary for an abstract class to have abstract methods only. However, if a class contains an abstract method, then it is an abstract class only.
Features of Abstract Class:
Here are some of the features of the abstract classes in Dart :
- One has to only use the ' abstract ' keyword to declare an abstract class.
- An abstract class can-not be initialized.
- An abstract class can be easily inherited. But, if we inherit an abstract class, we must ensure that all the abstract methods are provided with implementation.
- We can not create an object of the abstract class, but it can be extended.
Generally, the abstract classes are built to implement the abstract methods in the inheriting subclasses.
Given below is the syntax of declaring the abstract class :
abstract class class_name { // Body of the abstract class }
Overriding abstract method of an abstract class –
/* definition of the abstract class ' teacher ' that * contains the abstract methods */ abstract class teacher { /* declaring the abstract class methods in the * abstract class ' teacher ' */ void say( ) ; void write( ) ; } class student extends teacher { @override void say( ) { print( " Overriding the abstract method say( ) of the base class in the derived class ! " ) ; } @override void write( ) { print( " Overriding the abstract method write( ) of the base class in the derived class " ) ; } } int main( ) { student s1 = new student( ) ; s1.say( ) ; s1.write( ) ; return 0 ; }
Output :
Overriding the abstract method say( ) of the base class in the derived class !
Overriding the abstract method write( ) of the base class
Explanation :
First, we have created an abstract class ' teacher ' that contains two abstract methods ' say( ) ' and ' write( ) ' as its member functions. These functions were overridden in the derived class ' student ' inheriting the abstract class ' teacher '. We defined them to print some messages. After that, in the main( ) function, we created an object of the derived class ' student ' that invokes these functions and generates the respective output.
Note:
It is important to remember that we need not to override the abstract method in the derived class when only that class inherits from the abstract class.
Overriding abstract method of an abstract class in two different classes –
// creating the abstract class ' teacher ' with an abstract method display( )
abstract class teacher
{
// declaring the abstract method
void display( ) ;
}
// class ' student1 ' inheriting the abstract class ' teacher '
class student1 extends teacher
{
// Overriding method display( )
@override
void display( )
{
print( " Overriding the display( ) function of the base class ' teacher ' in the base class ' student1 ' " ) ;
}
}
// class ' student2 ' inheriting the abstract class ' teacher '
class student2 extends teacher
{
// Overriding method display( ) again
@override
void display( )
{
print( " Overriding the display( ) function of the base class ' teacher ' in the base class ' student2 ' " ) ;
}
}
void main( )
{
// creating the object of the class student1 and accessing the display( ) function
student1 s1 = new student1( ) ;
s1.display( ) ;
// creating the object of the class student2 and accessing the display( ) function
student2 s2 = new student2( ) ;
s2.display( ) ;
}
Output :
Overriding the display( ) function of the base class ' teacher ' in
the base class ' student1 '
Overriding the display( ) function of the base class ' teacher ' in
the base class ' student2 '
Explanation :
First, we have created an abstract class ' teacher ' that contains an abstract method display( ). This function is overridden in the two derived classes ' student1 ' and ' student2 ' inheriting the abstract class ' teacher '. We defined them to print some messages. After that, in the main( ) function, we created the objects of the derived class ' student1 ' and ' student2 ' that invokes these functions and generates the respective output.