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.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Geweldig!
Completion tarief verbeterd naar 11.11
Inheritance with extends
Veeg om het menu te tonen
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.
Bedankt voor je feedback!