Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Using Interfaces for Polymorphism | Composition and Polymorphism
Dart OOP Essentials

bookUsing Interfaces for Polymorphism

Polymorphism is a foundational concept in object-oriented programming that allows you to use different types of objects through a common interface. In Dart, interfaces enable this by letting unrelated classes implement the same set of methods, so you can write code that works with any object conforming to that interface. This means you can design flexible, reusable functions that operate on a wide variety of objects, as long as they implement the required interface.

main.dart

main.dart

copy
123456789101112131415161718192021222324252627282930313233
// Define the Drivable interface using an abstract class abstract class Drivable { void drive(); } // Car implements Drivable class Car implements Drivable { @override void drive() { print("The car is driving."); } } // Bike also implements Drivable class Bike implements Drivable { @override void drive() { print("The bike is driving."); } } // Function that accepts any Drivable void startJourney(Drivable vehicle) { vehicle.drive(); } void main() { Car car = Car(); Bike bike = Bike(); startJourney(car); // Output: The car is driving. startJourney(bike); // Output: The bike is driving. }

The startJourney function in this example demonstrates polymorphism in action. By accepting a parameter of type Drivable, it can operate on any object that implements the Drivable interface—regardless of whether it is a Car or a Bike. This means you can add new types of drivable vehicles in the future without changing the function, as long as they implement the interface. This approach leads to highly flexible and maintainable code, as your functions are decoupled from specific implementations and rely only on shared behavior.

main.dart

main.dart

copy
1234567891011121314151617181920212223242526272829303132333435
// main.dart // Define the Flyable interface using an abstract class abstract class Flyable { void fly(); } // Bird implements Flyable class Bird implements Flyable { @override void fly() { print("The bird is flying."); } } // Airplane also implements Flyable class Airplane implements Flyable { @override void fly() { print("The airplane is flying."); } } // Function that accepts any Flyable void launch(Flyable flyer) { flyer.fly(); } void main() { Bird bird = Bird(); Airplane airplane = Airplane(); launch(bird); // Output: The bird is flying. launch(airplane); // Output: The airplane is flying. }
Note
Note

Interfaces make your code more flexible and adaptable by allowing you to define contracts for behavior. This lets you swap out implementations easily, promote code reuse, and support testability in larger projects.

Defining interfaces
expand arrow

In Dart, every class defines an implicit interface. You can also define abstract classes with method signatures to act as interfaces. Any class that implements the interface must provide concrete implementations for all its methods.

Implementing multiple interfaces
expand arrow

Dart allows a class to implement multiple interfaces by separating them with commas. This means a single class can promise to provide the behavior of several different interfaces, increasing flexibility.

Polymorphic function parameters
expand arrow

When you declare a function parameter as an interface type, you can pass any object that implements that interface. This enables you to write functions that are agnostic to the specific class of the object, as long as it provides the required methods.

question mark

How do interfaces enable polymorphism in Dart?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you give an example of how to define and use an interface in Dart?

What are some real-world scenarios where polymorphism is especially useful?

How does Dart handle interfaces differently from other languages like Java or C#?

bookUsing Interfaces for Polymorphism

Desliza para mostrar el menú

Polymorphism is a foundational concept in object-oriented programming that allows you to use different types of objects through a common interface. In Dart, interfaces enable this by letting unrelated classes implement the same set of methods, so you can write code that works with any object conforming to that interface. This means you can design flexible, reusable functions that operate on a wide variety of objects, as long as they implement the required interface.

main.dart

main.dart

copy
123456789101112131415161718192021222324252627282930313233
// Define the Drivable interface using an abstract class abstract class Drivable { void drive(); } // Car implements Drivable class Car implements Drivable { @override void drive() { print("The car is driving."); } } // Bike also implements Drivable class Bike implements Drivable { @override void drive() { print("The bike is driving."); } } // Function that accepts any Drivable void startJourney(Drivable vehicle) { vehicle.drive(); } void main() { Car car = Car(); Bike bike = Bike(); startJourney(car); // Output: The car is driving. startJourney(bike); // Output: The bike is driving. }

The startJourney function in this example demonstrates polymorphism in action. By accepting a parameter of type Drivable, it can operate on any object that implements the Drivable interface—regardless of whether it is a Car or a Bike. This means you can add new types of drivable vehicles in the future without changing the function, as long as they implement the interface. This approach leads to highly flexible and maintainable code, as your functions are decoupled from specific implementations and rely only on shared behavior.

main.dart

main.dart

copy
1234567891011121314151617181920212223242526272829303132333435
// main.dart // Define the Flyable interface using an abstract class abstract class Flyable { void fly(); } // Bird implements Flyable class Bird implements Flyable { @override void fly() { print("The bird is flying."); } } // Airplane also implements Flyable class Airplane implements Flyable { @override void fly() { print("The airplane is flying."); } } // Function that accepts any Flyable void launch(Flyable flyer) { flyer.fly(); } void main() { Bird bird = Bird(); Airplane airplane = Airplane(); launch(bird); // Output: The bird is flying. launch(airplane); // Output: The airplane is flying. }
Note
Note

Interfaces make your code more flexible and adaptable by allowing you to define contracts for behavior. This lets you swap out implementations easily, promote code reuse, and support testability in larger projects.

Defining interfaces
expand arrow

In Dart, every class defines an implicit interface. You can also define abstract classes with method signatures to act as interfaces. Any class that implements the interface must provide concrete implementations for all its methods.

Implementing multiple interfaces
expand arrow

Dart allows a class to implement multiple interfaces by separating them with commas. This means a single class can promise to provide the behavior of several different interfaces, increasing flexibility.

Polymorphic function parameters
expand arrow

When you declare a function parameter as an interface type, you can pass any object that implements that interface. This enables you to write functions that are agnostic to the specific class of the object, as long as it provides the required methods.

question mark

How do interfaces enable polymorphism in Dart?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2
some-alt