Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of "objects," which encapsulate data and behavior. Dart, a language developed by Google, is no exception to this paradigm. In this article, we will delve into the fundamental concepts of OOP in Dart.
1. Classes and Objects:
At the heart of OOP in Dart are classes and objects. A class is a blueprint or a template that defines the structure and behavior of objects. Objects, on the other hand, are instances of classes. They encapsulate data and functionality.
class Car {
String make;
String model;
Car(this.make, this.model);
void start() {
print('$make $model is starting.');
}
}
void main() {
var myCar = Car('Toyota', 'Camry');
myCar.start();
}
In this example, Car
is a class with properties (make
and model
) and a method (start
). The main
function creates an instance of the Car
class and calls its start
method.
2. Encapsulation:
Dart supports encapsulation, which means bundling the data (attributes) and methods (functions) that operate on the data into a single unit, i.e., a class. This helps in controlling access to the data and ensures that the internal representation of an object is hidden from the outside world.
class BankAccount {
double _balance = 0;
void deposit(double amount) {
_balance += amount;
}
void withdraw(double amount) {
if (amount <= _balance) {
_balance -= amount;
} else {
print('Insufficient funds.');
}
}
double getBalance() {
return _balance;
}
}
In this example, _balance
is a private variable, and external code cannot access it directly. Access is provided through methods like deposit
, withdraw
, and getBalance
.
3. Inheritance:
Dart supports inheritance, allowing a class to inherit properties and methods from another class. The extends
keyword is used to achieve inheritance.
class Animal {
void eat() {
print('Animal is eating.');
}
}
class Dog extends Animal {
void bark() {
print('Dog is barking.');
}
}
Here, the Dog
class inherits the eat
method from the Animal
class.
4. Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of a common base class. Dart supports polymorphism through method overriding.
class Shape {
double getArea() {
return 0;
}
}
class Circle extends Shape {
double radius;
Circle(this.radius);
@override
double getArea() {
return 3.14 * radius * radius;
}
}
In this example, Circle
overrides the getArea
method from the base class Shape
.
5. Abstraction:
Abstraction involves hiding the implementation details and showing only the necessary features of an object. Dart supports abstraction through abstract classes and methods.
abstract class Shape {
double getArea();
}
An abstract class cannot be instantiated, but it can be used as a base class for other classes.
In conclusion, Dart's support for object-oriented programming provides developers with powerful tools to structure their code, promote reusability, and manage complexity. Understanding these OOP concepts in Dart is essential for building scalable and maintainable applications. As you explore Dart further, you'll find that OOP is a cornerstone of its design philosophy, enabling you to create robust and modular software solutions.