Composition vs Inheritance
Composition is an object-oriented design principle where a class contains other objects and uses them to perform its work instead of inheriting their behavior.
Composition helps you build complex features by combining simpler classes rather than extending a single parent class. In Dart, this means storing instances of other classes as fields and delegating responsibilities to them. Compared to inheritance, composition reduces tight coupling, improves flexibility, and makes your code easier to test and maintain.
- Inheritance models an "is-a" relationship: a
Dogis anAnimal. - Composition models a "has-a" relationship: a
Carhas anEngine.
main.dart
123456789101112131415161718192021class Engine { void start() { print('Engine started'); } } class Car { final Engine engine; Car(this.engine); void start() { engine.start(); } } void main() { Engine myEngine = Engine(); Car myCar = Car(myEngine); myCar.start(); // Output: Engine started }
In this example, the Car class does not inherit from Engine. Instead, it holds an instance of Engine and delegates the start() action to it. This is a classic use of composition: Car has-an Engine, and can use its functionality without forming a rigid inheritance relationship. You should prefer composition over inheritance when you want to use features from another class but do not want to expose your class to all behaviors of the parent, or when you want to change the implementation details without affecting a class hierarchy.
main.dart
123456789101112131415161718192021class Screen { void display() { print('Screen is displaying content'); } } class Phone { final Screen screen; Phone(this.screen); void showHomeScreen() { screen.display(); } } void main() { Screen phoneScreen = Screen(); Phone myPhone = Phone(phoneScreen); myPhone.showHomeScreen(); // Output: Screen is displaying content }
Favoring composition over inheritance means designing your classes to include (compose) other objects to share functionality, rather than always extending classes. This leads to more flexible, maintainable, and reusable code.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Fantastiskt!
Completion betyg förbättrat till 11.11
Composition vs Inheritance
Svep för att visa menyn
Composition is an object-oriented design principle where a class contains other objects and uses them to perform its work instead of inheriting their behavior.
Composition helps you build complex features by combining simpler classes rather than extending a single parent class. In Dart, this means storing instances of other classes as fields and delegating responsibilities to them. Compared to inheritance, composition reduces tight coupling, improves flexibility, and makes your code easier to test and maintain.
- Inheritance models an "is-a" relationship: a
Dogis anAnimal. - Composition models a "has-a" relationship: a
Carhas anEngine.
main.dart
123456789101112131415161718192021class Engine { void start() { print('Engine started'); } } class Car { final Engine engine; Car(this.engine); void start() { engine.start(); } } void main() { Engine myEngine = Engine(); Car myCar = Car(myEngine); myCar.start(); // Output: Engine started }
In this example, the Car class does not inherit from Engine. Instead, it holds an instance of Engine and delegates the start() action to it. This is a classic use of composition: Car has-an Engine, and can use its functionality without forming a rigid inheritance relationship. You should prefer composition over inheritance when you want to use features from another class but do not want to expose your class to all behaviors of the parent, or when you want to change the implementation details without affecting a class hierarchy.
main.dart
123456789101112131415161718192021class Screen { void display() { print('Screen is displaying content'); } } class Phone { final Screen screen; Phone(this.screen); void showHomeScreen() { screen.display(); } } void main() { Screen phoneScreen = Screen(); Phone myPhone = Phone(phoneScreen); myPhone.showHomeScreen(); // Output: Screen is displaying content }
Favoring composition over inheritance means designing your classes to include (compose) other objects to share functionality, rather than always extending classes. This leads to more flexible, maintainable, and reusable code.
Tack för dina kommentarer!