Menu
Coddy logo textTech

Abstract Methods

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 44 of 110.

An abstract method is a method declared inside an abstract class that has no body. It defines the method signature but leaves the implementation to each subclass. Any class that extends the abstract class must override all abstract methods, or it will also be abstract.

abstract class Animal {
  // Abstract method - no body, must be implemented by subclasses
  String makeSound();

  // Concrete method - has a body, shared by all subclasses
  void breathe() {
    print('Breathing...');
  }
}

class Dog extends Animal {
  @override
  String makeSound() {
    return 'Woof!';  // Must provide implementation
  }
}

class Cat extends Animal {
  @override
  String makeSound() {
    return 'Meow!';  // Different implementation, same signature
  }
}

If a subclass forgets to implement an abstract method, Dart will give you a compile error. This is the whole point - abstract methods enforce a contract. Every subclass is guaranteed to have that method, so you can safely call it on any instance of the abstract type.

// You can use the abstract type as a variable type
Animal a = Dog();
print(a.makeSound());  // Woof!

Animal b = Cat();
print(b.makeSound());  // Meow!

Abstract methods are different from concrete methods in an abstract class. Concrete methods have a body and are inherited as-is. Abstract methods have no body and must be overridden.

You can mix both in the same abstract class to share some behavior while forcing subclasses to define their own for others.

FeatureAbstract MethodConcrete Method
Has bodyNoYes
Must overrideYesNo
PurposeEnforce contractShare behavior
WhereAbstract class onlyAny class
challenge icon

Challenge

Easy

Let's build a shape calculator that demonstrates abstract methods in action. You'll define an abstract class with abstract methods that each shape must implement differently, then use those shapes through a common interface.

You'll organize your code into four files:

  • shape.dart: Define an abstract class Shape with a final field color (String) and a constructor that initializes it. Declare two abstract methods: double area() and double perimeter(). Also add a concrete method describe() that prints A [color] shape with area: [area] and perimeter: [perimeter]. Because area() and perimeter() are abstract, each subclass will provide its own calculation, but describe() works for all of them without being overridden.
  • circle.dart: Create a Circle class that extends Shape. It has a final field radius (double). Its constructor takes color and radius and passes color to super. Implement area() to return 3.14 * radius * radius and perimeter() to return 2 * 3.14 * radius.
  • rectangle.dart: Create a Rectangle class that extends Shape. It has final fields width and height (both double). Its constructor takes color, width, and height and passes color to super. Implement area() to return width * height and perimeter() to return 2 * (width + height).
  • main.dart: Import all files. Create a Circle with color 'red' and radius 5.0, and a Rectangle with color 'blue', width 4.0, and height 6.0. Call describe() on each shape. Then create a List<Shape> containing both shapes, loop through the list, and print Area: [area] for each shape.

Expected output:

A red shape with area: 78.5 and perimeter: 31.400000000000002
A blue shape with area: 24.0 and perimeter: 20.0
Area: 78.5
Area: 24.0

Cheat sheet

An abstract method is a method declared in an abstract class without a body. Subclasses must override it to provide their own implementation.

abstract class Animal {
  // Abstract method - no body
  String makeSound();

  // Concrete method - has a body
  void breathe() {
    print('Breathing...');
  }
}

Subclasses must implement all abstract methods:

class Dog extends Animal {
  @override
  String makeSound() {
    return 'Woof!';
  }
}

class Cat extends Animal {
  @override
  String makeSound() {
    return 'Meow!';
  }
}

You can use the abstract type as a variable type:

Animal a = Dog();
print(a.makeSound());  // Woof!

Animal b = Cat();
print(b.makeSound());  // Meow!
FeatureAbstract MethodConcrete Method
Has bodyNoYes
Must overrideYesNo
PurposeEnforce contractShare behavior
WhereAbstract class onlyAny class

Try it yourself

import 'shape.dart';
import 'circle.dart';
import 'rectangle.dart';

void main() {
  // TODO: Create a Circle with color 'red' and radius 5.0

  // TODO: Create a Rectangle with color 'blue', width 4.0, and height 6.0

  // TODO: Call describe() on the circle

  // TODO: Call describe() on the rectangle

  // TODO: Create a List<Shape> containing both shapes

  // TODO: Loop through the list and print "Area: [area]" for each shape
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming