Object-Oriented Programming (OOP) in Dart

OOP stands for Object-Oriented Programming. It is a way of writing code that uses "objects" — just like real things — to make programming easier and organized. Think of it like playing with LEGO blocks, where each block (object) has its own shape and function, and you can build cool stuff by combining them!

Full Forms & Simple Meanings:

1. Class and Object

class Animal {
  String name = 'Dog';

  void speak() {
    print('Woof!');
  }
}

void main() {
  var pet = Animal();
  print(pet.name);
  pet.speak();
}

Explanation: We created a class called Animal. Then we made an object called pet using that class. The object can talk (run the method) and has a name!

🔗 Dart Classes and Objects

2. Inheritance

class Animal {
  void sound() {
    print('Animal makes a sound');
  }
}

class Dog extends Animal {
  void bark() {
    print('Dog barks!');
  }
}

void main() {
  var dog = Dog();
  dog.sound();
  dog.bark();
}

Explanation: Dog gets features of Animal. This is called inheritance — like kids inheriting traits from parents.

🔗 Dart Inheritance

3. Encapsulation

class BankAccount {
  double _balance = 0; // private variable

  void deposit(double amount) {
    _balance += amount;
  }

  double get balance => _balance; // getter
}

void main() {
  var account = BankAccount();
  account.deposit(100);
  print(account.balance);
}

Explanation: Data is hidden inside the class and only accessed through functions. This is called encapsulation.

🔗 Encapsulation in Dart

4. Polymorphism

class Animal {
  void speak() {
    print("Animal sound");
  }
}

class Cat extends Animal {
  @override
  void speak() {
    print("Meow");
  }
}

void main() {
  Animal a = Cat();
  a.speak();
}

Explanation: speak() behaves differently based on the object. This is polymorphism — same method, different actions.

🔗 Polymorphism in Dart

5. Abstraction

abstract class Shape {
  void draw();
}

class Circle extends Shape {
  void draw() {
    print("Drawing Circle");
  }
}

void main() {
  Shape shape = Circle();
  shape.draw();
}

Explanation: Abstract class hides complex code and lets subclasses fill in the details. Just like giving someone a sketch and they color it in.

🔗 Abstraction in Dart

Summary