Skip to content

Flutter-Basic-To-Advanace/OOP

Repository files navigation

Dart & Flutter OOP Concepts

This repository contains examples of Object-Oriented Programming (OOP) concepts in Dart, which is the programming language used in Flutter.
Each concept is explained with clear code samples and detailed explanations.


📚 Concepts Covered

  1. Class and Object
  2. Constructor in Dart
  3. Access Modifiers
  4. Getter & Setter
  5. Static Members
  6. Inheritance
  7. Abstract Classes

1️⃣ Class and Object – class_and_object.dart

// class_and_object.dart

class Car {
  String brand = "Toyota";
  String model = "Corolla";

  void displayInfo() {
    print("Car: $brand $model");
  }
}

void main() {
  Car myCar = Car(); // Create object
  myCar.displayInfo();
}

💡 Explanation:

  • A class is a blueprint for creating objects.
  • Car is the class, and myCar is an instance (object).

2️⃣ Constructor in Dart – constructor.dart

// constructor.dart

class Car {
  String brand;
  String model;

  Car(this.brand, this.model); // Constructor

  void displayInfo() {
    print("Car: $brand $model");
  }
}

void main() {
  Car myCar = Car("Honda", "Civic");
  myCar.displayInfo();
}

💡 Explanation:

  • Constructors initialize objects when created.
  • this.brand assigns values directly from parameters.

3️⃣ Access Modifiers – access_modifiers.dart

// access_modifiers.dart

class BankAccount {
  String accountHolder;
  double _balance; // Private variable

  BankAccount(this.accountHolder, this._balance);

  void deposit(double amount) {
    _balance += amount;
    print("Deposited: $amount");
  }

  void showBalance() {
    print("Balance: $_balance");
  }
}

void main() {
  BankAccount account = BankAccount("John", 1000);
  account.deposit(500);
  account.showBalance();
}

💡 Explanation:

  • Dart uses underscore _ to make members private within the same file.
  • No keywords like private or public.

4️⃣ Getter & Setter – getter_setter.dart

// getter_setter.dart

class Student {
  String _name = "";
  int _age = 0;

  String get name => _name;

  set name(String newName) {
    if (newName.isNotEmpty) {
      _name = newName;
    }
  }

  int get age => _age;

  set age(int newAge) {
    if (newAge > 0) {
      _age = newAge;
    }
  }
}

void main() {
  Student student = Student();
  student.name = "Kamal"; // Setter
  student.age = 21;       // Setter
  print("Name: ${student.name}, Age: ${student.age}"); // Getter
}

💡 Explanation:

  • Getter returns a value.
  • Setter updates a value with validation logic.

5️⃣ Static Members – static.dart

// static.dart

class MathUtils {
  static double pi = 3.14159;

  static double square(double num) {
    return num * num;
  }
}

void main() {
  print("Pi: ${MathUtils.pi}");
  print("Square of 5: ${MathUtils.square(5)}");
}

💡 Explanation:

  • static means the member belongs to the class, not an instance.
  • Accessed using ClassName.memberName.

6️⃣ Inheritance – inheritance.dart

// inheritance.dart

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

class Dog extends Animal {
  @override
  void sound() {
    print("Dog barks");
  }
}

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

💡 Explanation:

  • extends means inheriting properties and methods from another class.
  • @override changes parent method behavior.

7️⃣ Abstract Classes – abstract_class.dart

// abstract_class.dart

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

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

void main() {
  Circle circle = Circle();
  circle.draw();
}

💡 Explanation:

  • Abstract classes cannot be instantiated.
  • They act as a blueprint for subclasses.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages