Dart Inheritance and Method Overriding in Dart: A Comprehensive Guide— Part5
Exploring Object-Oriented Dart: A Deep Dive into Inheritance and Method Overriding
1. Inheritance
Definition and Importance:
Inheritance is a core principle of object-oriented programming where a new class, called a subclass or derived class, can inherit properties and methods from an existing class, known as a superclass or base class.
Example Clarification:
In your example, the
Car
the class serves as the superclass, representing a generic car with aname
property and adrive
method.The
ElectricCar
class is a subclass ofCar
, inheriting its properties and methods while introducing an additionalchargeCapacity
property specific to electric cars.
Real-World Analogy:
An analogy to explain inheritance could be a “vehicle” superclass, with “car” and “truck” subclasses inheriting common features like wheels and engines but having their distinct properties and behaviors.
class Car {
String name;
Car(this.name);
void drive() => print("Driving a Car");
}
The Car
the class represents a basic car with a name
property and a drive
method.
class ElectricCar extends Car {
double chargeCapacity;
ElectricCar(String name, this.chargeCapacity) : super(name);
}
The
ElectricCar
the class extends theCar
class, inheriting its properties and methods. It introduces an additional propertychargeCapacity
.The
ElectricCar
class constructor initializes both thename
andchargeCapacity
properties using thesuper
keyword to call the superclass constructor.
2. Method Overriding
Definition and Purpose:
Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This enables subclasses to tailor behavior to their specific needs while maintaining a common interface.
Example Explanation:
In your code, the
drive
method is overridden in theElectricCar
subclass to provide a specialized message for electric vehicles.The
@override
annotation ensures that the method in the subclass is indeed overriding a method from its superclass, providing clarity and preventing accidental overrides.
Use Cases:
Method overriding is commonly used in scenarios where a subclass needs to customize behavior inherited from its superclass, such as providing specialized functionality for different types of vehicles.
class ElectricCar extends Car {
double chargeCapacity;
ElectricCar(String name, this.chargeCapacity) : super(name);
@override
void drive() => print('Driving an electric car');
}
- By adding the
@override
annotation, you explicitly indicate that you are overriding a method from the superclass.
Putting It All Together
Practical Demonstration:
Your code snippets effectively demonstrate how inheritance and method overriding work together in Dart.
By instantiating an
ElectricCar
object and invoking itsdrive
method, readers can witness firsthand how method overriding alters behavior based on subclass implementation.
Real-World Relevance:
Understanding inheritance and method overriding is essential for designing modular, maintainable, and extensible software systems, particularly in complex applications where classes and hierarchies abound
// Inheritance
void main() {
final electricVehicle = ElectricCar('TATA', 2900);
electricVehicle.drive();
}
class Car {
String name;
Car(this.name);
void drive() => print("Driving a Car");
}
class ElectricCar extends Car {
double chargeCapacity;
ElectricCar(String name, this.chargeCapacity) : super(name);
}
// Method Overriding
void main() {
final electricVehicle = ElectricCar('TATA', 2900);
print(electricVehicle.name);
electricVehicle.drive();
}
class Car {
String name;
Car(this.name);
void drive() => print("Driving a Car");
}
class ElectricCar extends Car {
double chargeCapacity;
ElectricCar(String name, this.chargeCapacity) : super(name);
@override
void drive() => print('Driving an electric car');
}
In the
main
function, we create an instance ofElectricCar
and call itsdrive
method.Since the
drive
method is overridden in theElectricCar
class, it prints "Driving an electric car".
This demonstrates how inheritance and method overriding work in Dart, allowing for code reuse and the ability to specialize behavior in subclasses. Understanding these concepts is crucial for building maintainable and extensible object-oriented Dart applications.
🌟 Stay Connected! 🌟
Hey there, awesome reader! 👋 Want to stay updated with my latest insights,Follow me on social media!
Sadanand Gadwal