Inheritance with extends
Inheritance is a core concept in object-oriented programming that allows you to create new classes based on existing ones. In Dart, you use the extends keyword to indicate that a class is inheriting from another class. The class being inherited from is called the base class (or superclass), while the class that inherits is called the derived class (or subclass). Inheritance enables you to reuse code and create hierarchies of related classes, making your programs more organized and reducing duplication.
main.dart
1234567891011121314class Animal { void speak() { print('Animal speaks'); } } class Dog extends Animal { // Inherits speak() from Animal } void main() { Dog dog = Dog(); dog.speak(); // Output: Animal speaks }
Dog is a subclass that extends Animal. This means Dog automatically inherits the speak() method from Animal, so you do not need to rewrite the method in the Dog class. When you create a Dog object and call speak(), it uses the inherited method from Animal. This approach reduces code duplication and makes your code easier to maintain, since shared behavior is defined only once in the base class.
main.dart
1234567891011121314151617class Vehicle { void move() { print('Vehicle is moving'); } } class Car extends Vehicle { void honk() { print('Car honks'); } } void main() { Car car = Car(); car.move(); // Output: Vehicle is moving car.honk(); // Output: Car honks }
While inheritance is a powerful tool, it can lead to problems when overused or when hierarchies become too deep. Deep inheritance chains can make code harder to understand and maintain. In some cases, using composition or interfaces is a better approach.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Can you show an example of how to define a base class and a subclass in Dart?
How can I override a method in the subclass if I want different behavior?
What are some real-world scenarios where inheritance is useful in Dart?
Génial!
Completion taux amélioré à 11.11
Inheritance with extends
Glissez pour afficher le menu
Inheritance is a core concept in object-oriented programming that allows you to create new classes based on existing ones. In Dart, you use the extends keyword to indicate that a class is inheriting from another class. The class being inherited from is called the base class (or superclass), while the class that inherits is called the derived class (or subclass). Inheritance enables you to reuse code and create hierarchies of related classes, making your programs more organized and reducing duplication.
main.dart
1234567891011121314class Animal { void speak() { print('Animal speaks'); } } class Dog extends Animal { // Inherits speak() from Animal } void main() { Dog dog = Dog(); dog.speak(); // Output: Animal speaks }
Dog is a subclass that extends Animal. This means Dog automatically inherits the speak() method from Animal, so you do not need to rewrite the method in the Dog class. When you create a Dog object and call speak(), it uses the inherited method from Animal. This approach reduces code duplication and makes your code easier to maintain, since shared behavior is defined only once in the base class.
main.dart
1234567891011121314151617class Vehicle { void move() { print('Vehicle is moving'); } } class Car extends Vehicle { void honk() { print('Car honks'); } } void main() { Car car = Car(); car.move(); // Output: Vehicle is moving car.honk(); // Output: Car honks }
While inheritance is a powerful tool, it can lead to problems when overused or when hierarchies become too deep. Deep inheritance chains can make code harder to understand and maintain. In some cases, using composition or interfaces is a better approach.
Merci pour vos commentaires !