Object-Oriented Programming (OOP) is a programming paradigm that focuses on organizing code by modeling real-world concepts as objects. Dart fully supports OOP principles, making it an effective language for creating structured and modular applications. Let’s explore the key OOP concepts in Dart:
1. Classes and Objects:
In Dart, a class is a blueprint or template that defines the structure and behavior of an object. An object, on the other hand, is an instance of a class. You can think of a class as a recipe, and an object as the actual dish created using that recipe.
dartCopy codeclass Person {
String name;
int age;
Person(this.name, this.age);
void greet() {
print("Hello, my name is $name and I am $age years old.");
}
}
void main() {
var person1 = Person("Alice", 30);
person1.greet(); // Output: Hello, my name is Alice and I am 30 years old.
}
2. Constructors:
Constructors are special methods used to create and initialize objects of a class. In Dart, a constructor with the same name as the class is used to create objects.
dartCopy codeclass Point {
int x, y;
Point(this.x, this.y);
}
void main() {
var point = Point(3, 5);
print("Point: (${point.x}, ${point.y})"); // Output: Point: (3, 5)
}
3. Inheritance:
Inheritance is a way to create a new class based on an existing class, inheriting its properties and behaviors. The new class is called a subclass or derived class, and the existing class is the superclass or base class.
dartCopy codeclass Animal {
void makeSound() {
print("Animal makes a sound");
}
}
class Dog extends Animal {
@override
void makeSound() {
print("Dog barks");
}
}
void main() {
var dog = Dog();
dog.makeSound(); // Output: Dog barks
}
4. Encapsulation:
Encapsulation involves bundling data (attributes) and methods (functions) that operate on the data within a single unit (class). This helps in hiding the internal details of the class from outside interference.
dartCopy codeclass BankAccount {
double _balance = 0;
void deposit(double amount) {
if (amount > 0) {
_balance += amount;
print("Deposited: $amount");
}
}
double get balance => _balance;
}
void main() {
var account = BankAccount();
account.deposit(100);
print("Balance: ${account.balance}"); // Output: Balance: 100
}
5. Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of a common superclass. Dart supports polymorphism through method overriding, where a subclass provides a specific implementation of a method declared in its superclass.
dartCopy codeclass Shape {
void draw() {
print("Drawing a shape");
}
}
class Circle extends Shape {
@override
void draw() {
print("Drawing a circle");
}
}
class Rectangle extends Shape {
@override
void draw() {
print("Drawing a rectangle");
}
}
void main() {
List<Shape> shapes = [Circle(), Rectangle()];
for (var shape in shapes) {
shape.draw();
}
// Output: Drawing a circle
// Drawing a rectangle
}
Conclusion:
Dart’s robust support for Object-Oriented Programming principles, including classes, objects, inheritance, encapsulation, and polymorphism, enables developers to create well-organized and modular applications. By understanding and utilizing these OOP concepts, you can build efficient and maintainable code that models real-world scenarios in a structured manner.