How Object-oriented programming (OOP) in Flutter using Dart
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code that operates on that data. The objects are instances of classes, which are essentially user-defined data types that encapsulate data and functions (methods) that operate on that data.
OOP provides a way to organize code and data in a more intuitive and reusable manner, making it easier to maintain and modify software over time. The principles of OOP include encapsulation, abstraction, inheritance, and polymorphism. These principles enable OOP to achieve modularity, extensibility, and maintainability.
Here's an example of OOP in Flutter using Dart, the programming language used in Flutter development:
class Car {
String make;
String model;
int year;
int speed = 0;
Car(this.make, this.model, this.year);
void accelerate() {
speed += 30;
}
void brake() {
speed -= 30;
}
void displaySpeed() {
print('Current speed: $speed');
}
}
void main() {
Car myCar = Car('Fiat', 'Honda', 2020);
myCar.accelerate();
myCar.displaySpeed(); // Current speed: 10
myCar.brake();
myCar.displaySpeed(); // Current speed: 0
}
The principles of object-oriented programming (OOP) in Flutter include:
- Abstraction
- Inheritance
- Polymorphism
- Encapsulation
- Classes and Objects
Abstraction:
Example:
abstract class Shape {
void draw();
}
class Circle extends Shape {
void draw() {
print('Drawing a Circle');
}
}
class Square extends Shape {
void draw() {
print('Drawing a Square');
}
}
void main() {
Shape circle = Circle();
Shape square = Square();
circle.draw(); // Drawing a Circle
square.draw(); // Drawing a Square
}
Inheritance:
class Animal {
String name;
int age;
Animal(this.name, this.age);
void makeSound() {
print('Animal Sound');
}
}
class Dog extends Animal {
String breed;
Dog(String name, int age, this.breed) : super(name, age);
@override
void makeSound() {
print('Bark!');
}
}
void main() {
Dog myDog = Dog('Max', 5, 'Labrador');
print(myDog.name); // Max
print(myDog.age); // 5
print(myDog.breed); // Labrador
myDog.makeSound(); // Bark!
}
Polymorphism:
Example:
abstract class Shape {
void draw();
}
class Circle extends Shape {
void draw() {
print('Drawing a Circle');
}
}
class Square extends Shape {
void draw() {
print('Drawing a Square');
}
}
void drawShapes(List<Shape> shapes) {
for (Shape shape in shapes) {
shape.draw();
}
}
void main() {
List<Shape> shapes = [Circle(), Square()];
drawShapes(shapes);
/*
Output:
Drawing a Circle
Drawing a Square
*/
}
Encapsulation:
Example:
class BankAccount {
String _accountHolder;
int _balance = 0;
BankAccount(this._accountHolder);
void deposit(int amount) {
_balance += amount;
}
void withdraw(int amount) {
_balance -= amount;
}
int getBalance() {
return _balance;
}
}
void main() {
BankAccount account = BankAccount('John Doe');
account.deposit(100);
account.withdraw(50);
print(account.getBalance()); // 50
}
Classes and Objects:
Example:
class Car {
String make;
String model;
int year;
Car(this.make, this.model, this.year);
void start() {
print('Car started.');
}
}
void main() {
Car myCar = Car('Toyota', 'Camry', 2020);
print(myCar.make); // Toyota
print(myCar.model); // Camry
print(myCar.year); // 2020
myCar.start(); // Car started.
}
Comments
Post a Comment