0% found this document useful (0 votes)
0 views

Object-Oriented Programming (OOP) in Dart

The document provides an overview of Object-Oriented Programming (OOP) in Dart, highlighting its key concepts such as classes, objects, encapsulation, inheritance, polymorphism, abstract classes, and mixins. It emphasizes the benefits of OOP including code reusability, modularity, maintainability, and scalability, particularly in the context of Flutter development. Additionally, the document includes examples and tasks to reinforce understanding of each OOP concept.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Object-Oriented Programming (OOP) in Dart

The document provides an overview of Object-Oriented Programming (OOP) in Dart, highlighting its key concepts such as classes, objects, encapsulation, inheritance, polymorphism, abstract classes, and mixins. It emphasizes the benefits of OOP including code reusability, modularity, maintainability, and scalability, particularly in the context of Flutter development. Additionally, the document includes examples and tasks to reinforce understanding of each OOP concept.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Object-Oriented Programming (OOP) in Dart

Introduction to OOP
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of
objects, which bundle data (attributes) and behavior (methods) together. It helps organize
code into reusable, maintainable structures. OOP is widely used in Flutter, as widgets
themselves are objects.

Why Use OOP?

●​ Code Reusability: Avoid duplication by reusing classes.


●​ Modularity: Code is organized into independent units.
●​ Maintainability: Easier to modify and debug code.
●​ Scalability: Large applications become manageable.

Real-World Example:

Think of a Car as an object:

●​ Attributes: Brand, model, speed, color.


●​ Methods: Start engine, accelerate, brake.

Instead of writing separate code for every car, we create a Car class and generate multiple
car objects from it.

1. Classes and Objects


A class is a blueprint for creating objects. An object is an instance of a class, holding real
values.

Theory:

●​ A class defines properties and behaviors.


●​ An object is a specific instance with actual values.

Example:
class Car {
String brand = "Toyota";
int speed = 0;

void accelerate() {
speed += 10;
print("The car is now going at $speed km/h");
}
}

void main() {
Car myCar = Car(); // Creating an object
print("Brand: ${myCar.brand}");
myCar.accelerate();
}

Task:

●​ Create a class Person with attributes name and age, and a method greet() that
prints a message.

2. Constructors
A constructor is a special method used to initialize an object when it is created.

Theory:

●​ A default constructor initializes values when an object is created.


●​ Parameterized constructors take arguments to set values.
●​ Named constructors allow multiple ways to initialize an object.

Example:
class Student {
String name;
int age;

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

void display() {
print("Student: $name, Age: $age");
}
}

void main() {
Student student1 = Student("Alice", 20);
student1.display();
}

Task:

●​ Modify the Person class to accept name and age through a constructor.
3. Encapsulation (Getters and Setters)
Encapsulation helps protect data by making variables private and exposing them via getters
and setters.

Theory:

●​ Private variables (using _ prefix) cannot be accessed directly.


●​ Getters retrieve values, setters modify them safely.

Example:
class BankAccount {
double _balance = 0; // Private variable

double get balance => _balance;

void deposit(double amount) {


_balance += amount;
print("Deposited: \$amount, New Balance: \$_balance");
}
}

void main() {
BankAccount account = BankAccount();
account.deposit(100);
print("Balance: \${account.balance}");
}

Task:

●​ Implement a Car class with private speed and a setter method to control
acceleration.

4. Inheritance
Inheritance allows a class to inherit properties and methods from another class.

Theory:

●​ Parent class properties/methods are available to child classes.


●​ The extends keyword is used to inherit.
●​ Helps in reusability and modularity.
Example:
class Animal {
void makeSound() {
print("Animal makes a sound");
}
}

class Dog extends Animal {


void bark() {
print("Dog barks");
}
}

void main() {
Dog dog = Dog();
dog.makeSound(); // Inherited method
dog.bark();
}

Task:

●​ Create a Vehicle class and inherit it in Car and Bike classes with unique methods.

5. Polymorphism (Method Overriding)


Polymorphism allows a subclass to provide a specific implementation of a method from its
superclass.

Theory:

●​ Method Overriding: A subclass changes the behavior of a parent class method.


●​ Helps in flexibility and dynamic method binding.

Example:
class Shape {
void draw() {
print("Drawing a shape");
}
}

class Circle extends Shape {


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

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

Task:

●​ Create a Bird class with a fly() method, then create a subclass Eagle that
overrides fly().

6. Abstract Classes & Interfaces


Abstract classes cannot be instantiated and can have abstract methods (without
implementation).

Example:
abstract class Animal {
void makeSound(); // Abstract method
}

class Cat extends Animal {


@override
void makeSound() {
print("Meow Meow");
}
}

void main() {
Cat cat = Cat();
cat.makeSound();
}

Task:

●​ Create an abstract class Appliance with an abstract method turnOn(), then


implement it in Fan and TV classes.

7. Mixins (Code Reuse Without Inheritance)


Mixins allow classes to share code without using inheritance.

Example:
mixin Logger {
void log(String message) {
print("LOG: $message");
}
}

class App with Logger {}

void main() {
App myApp = App();
myApp.log("Application started");
}

Task:

●​ Create a mixin SoundMixin and use it in Dog and Cat classes to add a
makeSound() method.

Conclusion
OOP in Dart is crucial for writing structured, reusable, and scalable code. Mastering OOP
will help in Flutter development, as Flutter widgets are built using Dart’s OOP principles.

Would you like exercises with real-world Flutter applications next? 🚀

You might also like