Method Overriding in Dart
In this lesson, you will learn about method overriding in Dart. Method overriding allows a child class to provide its own implementation of a method that is already defined in its parent class.
This is commonly used when different objects share a common structure but behave differently in real-world scenarios.
What Is Method Overriding?
Method overriding occurs when:
- A child class inherits from a parent class
- The child class defines a method with the same name
- The method has the same parameters as the parent method
Dart uses the @override annotation to clearly indicate
that a method is being overridden.
Real-World Example Scenario
Consider a company where different employees receive salary details in different formats.
All employees have basic information, but managers need additional details.
Parent Class Example
class Employee {
String name;
double salary;
Employee(this.name, this.salary);
void showDetails() {
print('Employee Name: $name');
print('Salary: $salary');
}
}
The Employee class provides a default implementation
of the showDetails() method.
Overriding the Method in Child Class
Now let’s override the method for a manager.
class Manager extends Employee {
double bonus;
Manager(String name, double salary, this.bonus)
: super(name, salary);
@override
void showDetails() {
print('--- Manager Details ---');
print('Name: $name');
print('Salary: $salary');
print('Bonus: $bonus');
}
}
Here, the Manager class provides its own implementation
of showDetails().
Using Overridden Methods
void main() {
Employee emp = Employee('Rahul', 55000);
emp.showDetails();
print('');
Manager mgr = Manager('Anita', 85000, 15000);
mgr.showDetails();
}
Although both objects use showDetails(),
each class executes its own version of the method.
Calling Parent Method Using super
Sometimes, you may want to extend the parent method instead of replacing it.
class SeniorManager extends Manager {
SeniorManager(String name, double salary, double bonus)
: super(name, salary, bonus);
@override
void showDetails() {
super.showDetails();
print('Level: Senior Management');
}
}
Why Method Overriding Is Important
- Allows customization of inherited behavior
- Supports polymorphism
- Makes applications flexible and scalable
- Improves real-world modeling of systems
Common Rules for Method Overriding
- Method name must be the same
- Parameters must match
- Return type must be compatible
- Use
@overridefor clarity
📝 Practice Exercises
Exercise 1
Create a parent class Vehicle with a method fuelType().
Exercise 2
Create a child class ElectricCar that overrides fuelType().
Exercise 3
Print different fuel types for different vehicles.
✅ Practice Answers
class Vehicle {
void fuelType() {
print('Fuel: Petrol');
}
}
class ElectricCar extends Vehicle {
@override
void fuelType() {
print('Fuel: Electricity');
}
}
void main() {
Vehicle v1 = Vehicle();
Vehicle v2 = ElectricCar();
v1.fuelType();
v2.fuelType();
}
What’s Next?
In the next lesson, you will learn about Abstract Classes — how to define incomplete classes and enforce structure across multiple child classes.