What is Inheritance in Dart?

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (known as a child or subclass) to inherit properties and methods from another class (known as a parent or superclass). In Dart, inheritance promotes code reuse and establishes a relationship between classes, enabling you to create a hierarchy of classes that share common functionality.

1. Basic Syntax of Inheritance

In Dart, you can create a subclass by using the extends keyword followed by the name of the superclass. The subclass inherits all the properties and methods of the superclass, and it can also define its own properties and methods.

Example of Inheritance

class Animal {
String name;

Animal(this.name); // Constructor

void speak() {
print('$name makes a sound.');
}
}

class Dog extends Animal {
Dog(String name) : super(name); // Calling the superclass constructor

// Overriding the speak method
@override
void speak() {
print('$name barks.');
}
}

void main() {
Dog myDog = Dog('Buddy');
myDog.speak(); // Output: Buddy barks.
}

In this example, we define a superclass Animal with a property name and a method speak. The subclass Dog extends Animal and overrides the speak method to provide a specific implementation for dogs. When we create an instance of Dog and call the speak method, it outputs "Buddy barks."

2. Accessing Superclass Members

In a subclass, you can access the properties and methods of the superclass using the super keyword. This is particularly useful when you want to call a method or constructor from the superclass.

Example of Accessing Superclass Members

class Animal {
String name;

Animal(this.name);

void speak() {
print('$name makes a sound.');
}
}

class Cat extends Animal {
Cat(String name) : super(name);

void display() {
super.speak(); // Calling the superclass method
print('$name is a cat.');
}
}

void main() {
Cat myCat = Cat('Whiskers');
myCat.display();
// Output: Whiskers makes a sound.
// Whiskers is a cat.
}

In this example, the Cat class extends the Animal class and has a method display that calls the speak method from the superclass using super.speak().

3. Multiple Inheritance

Dart does not support multiple inheritance directly (i.e., a class cannot extend more than one class). However, Dart allows you to implement multiple interfaces, which can provide similar functionality.

Example of Implementing Interfaces

class Animal {
void speak() {
print('Animal makes a sound.');
}
}

class Pet {
void play() {
print('Pet is playing.');
}
}

class Dog extends Animal implements Pet {
@override
void speak() {
print('Dog barks.');
}

@override
void play() {
print('Dog is playing.');
}
}

void main() {
Dog myDog = Dog();
myDog.speak(); // Output: Dog barks.
myDog.play(); // Output: Dog is playing.
}

In this example, the Dog class extends the Animal class and implements the Pet interface. This allows Dog to inherit behavior from Animal while also providing its own implementation of the play method from the Pet interface.

4. Conclusion

Inheritance in Dart is a powerful feature that allows you to create a hierarchy of classes, promoting code reuse and organization. By using inheritance, you can define common behavior in a superclass and extend or override that behavior in subclasses. Understanding how to use inheritance effectively is essential for building robust and maintainable Dart applications.