Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and methods from another class. This promotes code reusability and establishes a natural hierarchy between classes.
Key Concepts
- Base Class (Parent Class): The class whose properties and methods are inherited.
- Derived Class (Child Class): The class that inherits from the base class.
extends
Keyword: Used to indicate that a class is inheriting from another class.
Why Use Inheritance?
- Code Reusability: Avoids redundancy by reusing existing code.
- Hierarchical Classification: Establishes a relationship between base and derived classes.
- Maintainability: Easier to manage and update code.
Syntax
To create a derived class that inherits from a base class, use the extends
keyword.
class Animal { void eat() { print('Animal is eating'); } } class Dog extends Animal { void bark() { print('Dog is barking'); } } void main() { Dog dog = Dog(); dog.eat(); // Inherited method dog.bark(); // Method of Dog class }
Practical Example
Let's create a more detailed example involving a base class Vehicle
and a derived class Car
.
Base Class: Vehicle
class Vehicle { String brand; int year; Vehicle(this.brand, this.year); void displayInfo() { print('Brand: $brand, Year: $year'); } }
Derived Class: Car
class Car extends Vehicle { String model; Car(String brand, int year, this.model) : super(brand, year); void displayCarInfo() { displayInfo(); // Calling the method from the base class print('Model: $model'); } }
Main Function
Explanation
- Base Class (
Vehicle
): Contains propertiesbrand
andyear
, and a methoddisplayInfo()
. - Derived Class (
Car
): Inherits properties and methods fromVehicle
and adds a new propertymodel
and a methoddisplayCarInfo()
. - Constructor: The derived class constructor calls the base class constructor using
super
.
Exercises
Exercise 1: Create a Base and Derived Class
- Create a base class
Person
with propertiesname
andage
, and a methoddisplayPersonInfo()
. - Create a derived class
Student
that inherits fromPerson
and adds a propertystudentId
. - Add a method
displayStudentInfo()
in theStudent
class that callsdisplayPersonInfo()
and also prints thestudentId
.
Solution
class Person { String name; int age; Person(this.name, this.age); void displayPersonInfo() { print('Name: $name, Age: $age'); } } class Student extends Person { String studentId; Student(String name, int age, this.studentId) : super(name, age); void displayStudentInfo() { displayPersonInfo(); // Calling the method from the base class print('Student ID: $studentId'); } } void main() { Student student = Student('Alice', 20, 'S12345'); student.displayStudentInfo(); }
Exercise 2: Override a Method
- Create a base class
Shape
with a methodarea()
that returns 0. - Create a derived class
Rectangle
that overrides thearea()
method to return the area of the rectangle.
Solution
class Shape { double area() { return 0; } } class Rectangle extends Shape { double width; double height; Rectangle(this.width, this.height); @override double area() { return width * height; } } void main() { Rectangle rectangle = Rectangle(5, 10); print('Area of Rectangle: ${rectangle.area()}'); }
Common Mistakes and Tips
- Forgetting to Call
super
: When a derived class has a constructor, ensure to call the base class constructor usingsuper
. - Overriding Methods: Use the
@override
annotation to indicate that a method is being overridden. This helps in catching errors during compilation. - Accessing Base Class Members: Use
super
to access methods and properties of the base class if they are overridden in the derived class.
Conclusion
Inheritance is a powerful feature in Dart that allows you to create a hierarchy of classes, promoting code reuse and maintainability. By understanding and utilizing inheritance, you can write more efficient and organized code. In the next section, we will explore Mixins, another way to reuse code in Dart.
Dart Programming Course
Module 1: Introduction to Dart
- Introduction to Dart
- Setting Up the Development Environment
- Your First Dart Program
- Basic Syntax and Structure