Dart OOP Concepts
Understanding Object-Oriented Programming in Dart
🎯 What is OOP in Dart?
Dart is an object-oriented language where everything is an object. OOP helps organize code using classes, objects, inheritance, and encapsulation, making Flutter apps easier to build and maintain.
// Simple class example
class Person {
String name = 'John';
void sayHello() {
print('Hello, I am $name');
}
}
Output:
Hello, I am John
Key OOP Concepts
Classes
Blueprint for creating objects
class Car {
String brand = 'Toyota';
}
Objects
Instances of classes
var myCar = Car();
print(myCar.brand);
Encapsulation
Hide data using private variables
class Account {
double _balance = 0;
}
Inheritance
Reuse code from parent classes
class Animal {}
class Dog extends Animal {}
🔹 Classes and Objects
Classes define the structure, objects are instances:
// Define a class
class Student {
String name;
int age;
// Constructor
Student(this.name, this.age);
// Method
void displayInfo() {
print('Name: $name, Age: $age');
}
}
// Create objects
void main() {
var student1 = Student('Alice', 20);
var student2 = Student('Bob', 22);
student1.displayInfo();
student2.displayInfo();
}
Output:
Name: Alice, Age: 20
Name: Bob, Age: 22
🔹 Constructors
Special methods to initialize objects:
class Phone {
String brand;
double price;
// Default constructor
Phone(this.brand, this.price);
// Named constructor
Phone.samsung() : brand = 'Samsung', price = 799.99;
// Named constructor with parameters
Phone.custom({required this.brand, required this.price});
}
void main() {
var phone1 = Phone('iPhone', 999.99);
var phone2 = Phone.samsung();
var phone3 = Phone.custom(brand: 'Google', price: 699.99);
}
🔹 Encapsulation
Use private variables (prefix with _) to protect data:
class BankAccount {
String _accountNumber;
double _balance = 0;
BankAccount(this._accountNumber);
// Getter
double get balance => _balance;
// Setter with validation
set balance(double amount) {
if (amount >= 0) {
_balance = amount;
}
}
void deposit(double amount) {
_balance += amount;
print('Deposited: \$$amount');
}
}
void main() {
var account = BankAccount('123456');
account.deposit(100);
print('Balance: \$${account.balance}');
}
Output:
Deposited: $100
Balance: $100
🔹 Inheritance
Create new classes based on existing ones:
// Parent class
class Animal {
String name;
Animal(this.name);
void makeSound() {
print('$name makes a sound');
}
}
// Child class
class Dog extends Animal {
Dog(String name) : super(name);
@override
void makeSound() {
print('$name barks: Woof! Woof!');
}
void fetch() {
print('$name is fetching the ball');
}
}
void main() {
var dog = Dog('Buddy');
dog.makeSound();
dog.fetch();
}
Output:
Buddy barks: Woof! Woof!
Buddy is fetching the ball
🔹 Abstract Classes
Define blueprints that cannot be instantiated:
// Abstract class
abstract class Shape {
// Abstract method
double calculateArea();
// Regular method
void display() {
print('Area: ${calculateArea()}');
}
}
class Circle extends Shape {
double radius;
Circle(this.radius);
@override
double calculateArea() {
return 3.14 * radius * radius;
}
}
void main() {
var circle = Circle(5);
circle.display();
}
Output:
Area: 78.5
🔹 Interfaces (Implicit)
Every class defines an interface in Dart:
class Printer {
void printDocument() {
print('Printing document...');
}
}
class Scanner {
void scanDocument() {
print('Scanning document...');
}
}
// Implement multiple interfaces
class AllInOne implements Printer, Scanner {
@override
void printDocument() {
print('All-in-One: Printing...');
}
@override
void scanDocument() {
print('All-in-One: Scanning...');
}
}
void main() {
var device = AllInOne();
device.printDocument();
device.scanDocument();
}
Output:
All-in-One: Printing...
All-in-One: Scanning...