— Dart, Abstract Classes, Interfaces — 2 min read
Dart is an object-oriented programming language that provides support for abstract classes and interfaces. These are important tools that can help you write more modular, reusable, and testable code. In this article, we will explore what abstract classes and interfaces are and how to use them in your Dart code.
An abstract class is a class that cannot be instantiated directly. Instead, it serves as a base or template for other classes to inherit from. Abstract classes provide a way to define a common interface for a set of related classes. They are similar to interfaces, but they can also contain method implementations.
To define an abstract class in Dart, you use the abstract
keyword before the class declaration. Here is an example:
1abstract class Animal {2 void makeSound();3}4
5class Dog extends Animal {6 void makeSound() {7 print('Woof!');8 }9}10
11class Cat extends Animal {12 void makeSound() {13 print('Meow!');14 }15}
In this example, Animal
is an abstract class that defines a single abstract method makeSound()
. The Dog
and Cat
classes extend the Animal
class and implement the makeSound()
method. Note that the Dog
and Cat
classes must implement the makeSound()
method because it is defined as abstract in the Animal
class.
You cannot create an instance of an abstract class directly, but you can create a reference to it. For example:
1Animal myPet = new Dog();2myPet.makeSound(); // Output: Woof!
Here, we create a reference to an Animal
object and assign it to a Dog
instance. When we call makeSound()
on the myPet
object, it calls the makeSound()
method implemented in the Dog
class.
An interface is a contract that specifies a set of methods and properties that a class must implement. Unlike abstract classes, interfaces cannot contain method implementations – they only define the signature of the methods. A class can implement multiple interfaces, but it can only extend one class.
To define an interface in Dart, you use the implements
keyword followed by the interface name. Here is an example:
1abstract class Animal {2 void makeSound();3}4
5abstract class Flyable {6 void fly();7}8
9class Bird extends Animal implements Flyable {10 void makeSound() {11 print('Chirp!');12 }13
14 void fly() {15 print('Flap flap!');16 }17}
In this example, we define two abstract classes Animal
and Flyable
. We also define a Bird
class that extends Animal
and implements Flyable
. The Bird
class must implement both the makeSound()
method from the Animal
class and the fly()
method from the Flyable
class.
You can also use interfaces as type annotations for variables and parameters. For example:
1void letItFly(Flyable flyingObject) {2 flyingObject.fly();3}4
5Flyable myAirplane = new Airplane();6letItFly(myAirplane);
Here, we define a function letItFly()
that takes a parameter of type Flyable
. We create an instance of the Airplane
class, which implements Flyable
, and pass it to the letItFly()
function.
Abstract classes and interfaces are important concepts in object-oriented programming that can help you write more modular, reusable, and testable code. In Dart, you can use abstract classes to define a common interface for a set of related classes and interfaces to specify a set of methods and properties that a class must implement. By using these tools effectively, you can create more robust and maintainable code in your Dart projects.