paint-brush
Exploring Dart Fundamentals - Part 5: Inheritance and Method Overridingby@sadanandgadwal
124 reads

Exploring Dart Fundamentals - Part 5: Inheritance and Method Overriding

by Sadanand GadwalApril 23rd, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Inheritance allows subclasses to inherit properties and methods from superclasses, promoting code reuse. Method overriding lets subclasses customize inherited methods, enhancing flexibility and maintainability. These concepts are fundamental in object-oriented programming, especially in languages like Dart.
featured image - Exploring Dart Fundamentals - Part 5: Inheritance and Method Overriding
Sadanand Gadwal HackerNoon profile picture


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 a name property and a drive method.
  • The ElectricCar class is a subclass of Car, inheriting its properties and methods while introducing an additional chargeCapacity 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 the Car class, inheriting its properties and methods. It introduces an additional property chargeCapacity.
  • The ElectricCar class constructor initializes both the name and chargeCapacity properties using the super 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 the ElectricCar 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 its drive 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 of ElectricCar and call its drive method.

  • Since the drive method is overridden in the ElectricCar 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 sub classes. 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