> For the complete documentation index, see [llms.txt](https://des-vu-technologies.gitbook.io/blogs-by-shankar/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://des-vu-technologies.gitbook.io/blogs-by-shankar/oop-with-dart/oopwithdart.md).

# oopWithDart

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to organize and structure code. Dart is a modern language that fully supports OOP principles. Here's an overview of OOP concepts in Dart:

1. **Classes and Objects:**

   * In Dart, everything is an object, and classes define the blueprint for creating objects.
   * Classes encapsulate data (attributes) and behavior (methods) into a single unit.
   * Objects are instances of classes and represent real-world entities.

   ```dart
   class Person {
     String name;
     int age;

     void introduce() {
       print('My name is $name, and I am $age years old.');
     }
   }

   void main() {
     var person = Person();
     person.name = 'Alice';
     person.age = 30;
     person.introduce();
   }
   ```
2. **Constructor:**

   * Constructors are special methods used to initialize objects when they are created.
   * Dart provides a default constructor if one is not defined.

   ```dart
   class Person {
     String name;
     int age;

     // Parameterized constructor
     Person(this.name, this.age);

     void introduce() {
       print('My name is $name, and I am $age years old.');
     }
   }

   void main() {
     var person = Person('Alice', 30);
     person.introduce();
   }
   ```
3. **Inheritance:**

   * Dart supports single inheritance, where a class can inherit properties and methods from a single parent class.
   * The `extends` keyword is used for inheritance.

   ```dart
   class Student extends Person {
     String school;

     Student(String name, int age, this.school) : super(name, age);

     void study() {
       print('$name is studying at $school.');
     }
   }

   void main() {
     var student = Student('Bob', 20, 'ABC School');
     student.introduce(); // Inherited from Person class
     student.study();
   }
   ```
4. **Polymorphism:**

   * Polymorphism allows objects of different classes to be treated as objects of a common superclass.
   * Method overriding enables subclass methods to provide a specific implementation.

   ```dart
   class Shape {
     void draw() {
       print('Drawing a shape.');
     }
   }

   class Circle extends Shape {
     @override
     void draw() {
       print('Drawing a circle.');
     }
   }

   void main() {
     Shape shape = Circle();
     shape.draw(); // Calls Circle's draw method
   }
   ```
5. **Encapsulation:**

   * Encapsulation is the practice of hiding the internal details of an object and exposing only what is necessary.
   * Dart uses private and public access modifiers (`_` and `final`/`var`) to control visibility.

   ```dart
   class BankAccount {
     double _balance = 0.0; // Private field

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

     double getBalance() {
       return _balance;
     }
   }
   ```
6. **Abstraction:**

   * Abstraction allows you to define the structure of a class without providing a complete implementation.
   * Abstract classes and methods are defined using the `abstract` keyword.

   ```dart
   abstract class Shape {
     void draw(); // Abstract method
   }

   class Circle extends Shape {
     @override
     void draw() {
       print('Drawing a circle.');
     }
   }
   ```

These are the core OOP concepts in Dart. You can use them to create well-structured and maintainable code, making it easier to model real-world entities and their interactions in your Dart applications.
