Member-only story
Inheritance in Dart
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit properties and methods from another class. In Dart, the extends keyword is used to achieve inheritance.
Read Medium Story for Free 👇
Basic Inheritance
A child class can inherit properties and methods from a parent class using extends.
class Animal {
void makeSound() {
print("Animal makes a sound");
}
}
class Dog extends Animal {
void bark() {
print("Dog barks");
}
}
void main() {
var dog = Dog();
dog.makeSound(); // Inherited method
dog.bark(); // Dog-specific method
}Overriding Methods
A subclass can override a parent class method using the @override annotation.
class Animal {
void makeSound() {
print("Animal makes a sound");
}
}
class Cat extends Animal {
@override
void makeSound() {
print("Cat meows");
}
}
void main() {
var cat = Cat();
cat.makeSound(); // Output: Cat meows
}









