Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Inheritance with extends | Inheritance and Abstraction
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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

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?

bookInheritance with extends

Svep för att visa menyn

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 1
some-alt