Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code: data in the form of fields (often known as properties or attributes), and code in the form of procedures (often known as methods). Dart is an object-oriented language, and understanding OOP principles is crucial for effective Dart programming.

Key Concepts of OOP

  1. Classes and Objects
  2. Inheritance
  3. Polymorphism
  4. Encapsulation
  5. Abstraction

  1. Classes and Objects

A class is a blueprint for creating objects. An object is an instance of a class. Classes encapsulate data for the object and methods to manipulate that data.

Example:

class Animal {
  String name;
  int age;

  // Constructor
  Animal(this.name, this.age);

  // Method
  void makeSound() {
    print('$name makes a sound.');
  }
}

void main() {
  // Creating an object of the Animal class
  Animal dog = Animal('Dog', 3);
  dog.makeSound(); // Output: Dog makes a sound.
}

  1. Inheritance

Inheritance allows a class to inherit properties and methods from another class. The class that inherits is called the subclass, and the class being inherited from is called the superclass.

Example:

class Animal {
  String name;
  int age;

  Animal(this.name, this.age);

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

// Dog class inherits from Animal class
class Dog extends Animal {
  Dog(String name, int age) : super(name, age);

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

void main() {
  Dog dog = Dog('Dog', 3);
  dog.makeSound(); // Output: Dog barks.
}

  1. Polymorphism

Polymorphism allows methods to do different things based on the object it is acting upon. It is often achieved through method overriding.

Example:

class Animal {
  String name;
  int age;

  Animal(this.name, this.age);

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

class Dog extends Animal {
  Dog(String name, int age) : super(name, age);

  @override
  void makeSound() {
    print('$name barks.');
  }
}

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

  @override
  void makeSound() {
    print('$name meows.');
  }
}

void main() {
  Animal dog = Dog('Dog', 3);
  Animal cat = Cat('Cat', 2);

  dog.makeSound(); // Output: Dog barks.
  cat.makeSound(); // Output: Cat meows.
}

  1. Encapsulation

Encapsulation is the concept of wrapping data and methods that work on the data within one unit, e.g., a class. It restricts direct access to some of the object's components, which can prevent the accidental modification of data.

Example:

class Animal {
  String _name; // Private variable
  int _age; // Private variable

  Animal(this._name, this._age);

  // Getter for name
  String get name => _name;

  // Setter for name
  set name(String name) {
    _name = name;
  }

  void makeSound() {
    print('$_name makes a sound.');
  }
}

void main() {
  Animal dog = Animal('Dog', 3);
  dog.makeSound(); // Output: Dog makes a sound.
  dog.name = 'New Dog';
  print(dog.name); // Output: New Dog
}

  1. Abstraction

Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object. It can be achieved using abstract classes and interfaces.

Example:

abstract class Animal {
  String name;
  int age;

  Animal(this.name, this.age);

  void makeSound(); // Abstract method
}

class Dog extends Animal {
  Dog(String name, int age) : super(name, age);

  @override
  void makeSound() {
    print('$name barks.');
  }
}

void main() {
  Dog dog = Dog('Dog', 3);
  dog.makeSound(); // Output: Dog barks.
}

Practical Exercises

Exercise 1: Create a Class

Create a class Car with properties brand, model, and year. Add a method displayInfo that prints the car's details.

Solution:

class Car {
  String brand;
  String model;
  int year;

  Car(this.brand, this.model, this.year);

  void displayInfo() {
    print('Car: $brand $model, Year: $year');
  }
}

void main() {
  Car car = Car('Toyota', 'Corolla', 2020);
  car.displayInfo(); // Output: Car: Toyota Corolla, Year: 2020
}

Exercise 2: Implement Inheritance

Create a class ElectricCar that inherits from Car and adds a property batteryLife. Override the displayInfo method to include the battery life.

Solution:

class Car {
  String brand;
  String model;
  int year;

  Car(this.brand, this.model, this.year);

  void displayInfo() {
    print('Car: $brand $model, Year: $year');
  }
}

class ElectricCar extends Car {
  int batteryLife;

  ElectricCar(String brand, String model, int year, this.batteryLife) : super(brand, model, year);

  @override
  void displayInfo() {
    print('Electric Car: $brand $model, Year: $year, Battery Life: $batteryLife hours');
  }
}

void main() {
  ElectricCar eCar = ElectricCar('Tesla', 'Model S', 2021, 24);
  eCar.displayInfo(); // Output: Electric Car: Tesla Model S, Year: 2021, Battery Life: 24 hours
}

Conclusion

In this section, we covered the fundamental concepts of Object-Oriented Programming in Dart, including classes and objects, inheritance, polymorphism, encapsulation, and abstraction. Understanding these concepts is crucial for building robust and maintainable applications in Dart. In the next module, we will dive deeper into Flutter widgets, which are the building blocks of any Flutter application.

Flutter Development Course

Module 1: Introduction to Flutter

Module 2: Dart Programming Basics

Module 3: Flutter Widgets

Module 4: State Management

Module 5: Navigation and Routing

Module 6: Networking and APIs

Module 7: Persistence and Storage

Module 8: Advanced Flutter Concepts

Module 9: Testing and Debugging

Module 10: Deployment and Maintenance

Module 11: Flutter for Web and Desktop

© Copyright 2024. All rights reserved