Abstract Classes and Interfaces – Flutter

Santhosh Adiga U
5 min readMar 17, 2023

--

Abstract classes and interfaces are two different concepts in object-oriented programming that serve similar purposes but are used in slightly different ways.

An abstract class is a class that cannot be instantiated on its own, but instead is meant to be subclassed by other classes. It can contain both abstract methods, which are defined but not implemented, as well as concrete methods, which have an implementation. A concrete subclass of an abstract class must implement all abstract methods and can optionally override concrete methods.

An interface, on the other hand, is a set of methods that a class must implement in order to conform to that interface. It doesn’t contain any implementations of its own, but simply defines the required methods that must be implemented. A class can implement multiple interfaces, allowing it to be used in different contexts as needed.

In Flutter, both abstract classes and interfaces can be used to define a common set of functionality that can be shared by multiple classes. For example, an abstract class could define a set of properties and methods that a group of related classes should all have, while an interface could define a set of methods that a class must implement in order to be used in a particular context.

By using abstract classes and interfaces effectively in your Flutter code, you can create more modular, reusable, and flexible code that is easier to maintain and extend over time.

For example:

1. Consider the following abstract class:

abstract class Animal {
String name;

void speak();
}

This abstract class defines two properties, name and speak(). Any class that extends this abstract class must implement the speak() method.

class Dog extends Animal {
@override
void speak() {
print('Woof!');
}
}

class Cat extends Animal {
@override
void speak() {
print('Meow!');
}
}

In this example, we have two concrete classes, Dog and Cat, that extend the Animal abstract class. They both implement the speak() method, but they can have their own unique implementations.

2. Consider the following interface:

abstract class IShape {
void draw();
void resize();
}

This interface defines two methods, draw() and resize(). Any class that implements this interface must provide its own implementation for these methods.

class Circle implements IShape {
@override
void draw() {
print('Drawing a Circle');
}

@override
void resize() {
print('Resizing a Circle');
}
}

class Square implements IShape {
@override
void draw() {
print('Drawing a Square');
}

@override
void resize() {
print('Resizing a Square');
}
}

In this example, we have two classes, Circle and Square, that implement the IShape interface. They both provide their own implementation for the draw() and resize() methods.

Using Abstract Classes and Interfaces Efficiently in Flutter

Abstract classes and interfaces are powerful tools for building modular and reusable code in Flutter. Here are some best practices for using them efficiently:

Use abstract classes to define a base implementation for a set of related classes.

Use interfaces to define a set of methods that a class must implement.

Combine abstract classes and interfaces to create flexible and reusable code.

When implementing an interface, only implement the methods that are necessary for your use case.

Use the implements keyword to implement an interface and the extends keyword to extend an abstract class.

Use the super keyword to call methods from the superclass.

Here’s an example that demonstrates how to use abstract classes and interfaces together in a Flutter application:

// Define an interface for objects that can be resized
abstract class Resizable {
void resize();
}

// Define an abstract class for shapes
abstract class Shape {
void draw();
}

// Define a concrete subclass of Shape for circles
class Circle extends Shape implements Resizable {
void draw() {
print('Drawing a Circle');
}

void resize() {
print('Resizing a Circle');
}
}

// Define a concrete subclass of Shape for squares
class Square extends Shape implements Resizable {
void draw() {
print('Drawing a Square');
}

void resize() {
print('Resizing a Square');
}
}

// Define a class that can draw and resize shapes
class Canvas {
void drawShape(Shape shape) {
shape.draw();
}

void resizeShape(Resizable shape) {
shape.resize();
}
}

void main() {
// Create a canvas object
final canvas = Canvas();

// Create some shapes to draw and resize
final circle = Circle();
final square = Square();

// Draw and resize the shapes
canvas.drawShape(circle);
canvas.resizeShape(circle);

canvas.drawShape(square);
canvas.resizeShape(square);
}

In this example, we have an interface called Resizable that defines a method called resize(), and an abstract class called Shape that defines a method called draw(). We then have two concrete classes, Circle and Square, that extend the Shape abstract class and implement the Resizable interface.

Finally, we have a Canvas class that can draw and resize shapes. The drawShape() method takes a Shape object as an argument, while the resizeShape() method takes a Resizable object as an argument. This allows us to draw and resize any shape that implements the Shape abstract class and the Resizable interface, making our code more modular and reusable.

Conclusion

In Flutter, abstract classes and interfaces are powerful tools that can help you write more modular, reusable, and flexible code. Abstract classes allow you to define a common set of functionality that can be shared by multiple classes, while interfaces define a set of methods that a class must implement to be used in a particular context.

By using abstract classes and interfaces effectively in your Flutter code, you can create code that is easier to maintain and extend over time. You can create abstract classes that define the common properties and methods of a group of related classes, and use them as a base class for concrete classes that provide specific implementations. You can also create interfaces that define a set of methods that a class must implement to be used in a particular context, making your code more modular and flexible.

Overall, abstract classes and interfaces are essential tools for any Flutter developer who wants to write clean, maintainable, and extensible code. By mastering these concepts and using them effectively, you can take your Flutter development skills to the next level.

--

--