Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Inheritance with extends | Inheritance and Abstraction
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Dart OOP Essentials

bookInheritance 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

main.dart

copy
1234567891011121314
class 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

main.dart

copy
1234567891011121314151617
class 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 }
Note
Study More

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.

question mark

What is the primary benefit of using inheritance in object-oriented programming?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

bookInheritance 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

main.dart

copy
1234567891011121314
class 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

main.dart

copy
1234567891011121314151617
class 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 }
Note
Study More

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.

question mark

What is the primary benefit of using inheritance in object-oriented programming?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 1
some-alt