Dart Interfaces: Extending vs Implementing | Difference between Extends & Implements keyword

1. Extending (Extends keyword)
Extending is used to create a subclass that inherits the properties and methods of a superclass.
It establishes an "is-a" relationship between the subclass and the superclass. For example, if you have a class
Animaland another classDogthat extendsAnimal, you're saying thatDogis a specific type ofAnimal.The subclass can access all non-private members (fields and methods) of the superclass.
You use the
extendskeyword to indicate that a class inherits from another class.Example:
class Animal { void eat() { print('Animal is eating'); } } class Dog extends Animal { void bark() { print('Dog is barking'); } }In this example,
DogextendsAnimal, indicating that aDogis a specific type ofAnimaland inherits theeat()method.
2. Implementing (implements keyword)
Implementing is used to declare that a class will provide specific behavior as defined by an interface.
It establishes a "can-do" relationship between the implementing class and the interface.
An interface in Dart is effectively a class with abstract methods and possibly no implementation.
A class can implement multiple interfaces.
You use the
implementskeyword to declare that a class implements one or more interfaces.Example:
abstract class Flyable { void fly(); } // Define a class named Bird which implements the Flyable interface class Bird implements Flyable { @override void fly() { print('Bird is flying'); } }In this example, the
Birdclass implements theFlyableinterface, which means that it agrees to provide an implementation for thefly()method defined in theFlyableinterface.- In summary, extending is used for inheritance, where a subclass inherits properties and methods from a superclass, while implementing is used to fulfill a contract defined by an interface, providing specific behavior as required by that interface.



