Flutter is an open-source UI software development kit (SDK) developed by Google, designed to help users easily create modern, high-performance applications. In this course, we will delve deeply into the fundamental concepts of Object-Oriented Programming (OOP), including class, object, and instance. These concepts are essential for structuring and managing Flutter applications.
1. What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is one of the programming paradigms that manages data by grouping it as objects. An object includes state and behavior, and defining these objects is precisely what a class does. The main features of OOP are as follows:
- Encapsulation: Bundles the object’s properties and methods to provide insulation from outside.
- Inheritance: Promotes code reuse by defining new classes based on existing classes.
- Polymorphism: Defines interfaces that allow different classes to behave in the same way.
- Abstraction: Simplifies complex systems efficiently to make them easier to handle.
Now, let’s look at how these OOP concepts are utilized in Flutter.
2. Class
A class is a template for creating objects. A class can define data variables and may include methods that manipulate that data. Here is how to define a class in Flutter:
class Car {
String color;
String model;
Car(this.color, this.model);
void display() {
print('Car Model: $model, Color: $color');
}
}
In the example above, we defined a class called Car
. This class has two properties, color
and model
, which are initialized through the constructor. The display
method prints the car’s information. Next, let’s create objects using this class.
3. Object and Instance
An object refers to an instance of a class. In other words, it refers to a real set of data created from a class. You can create multiple objects, each having unique states. For example, we can create instances of the Car
class as follows:
void main() {
Car car1 = Car('red', 'sports car');
Car car2 = Car('blue', 'sedan');
car1.display(); // Output: Car Model: sports car, Color: red
car2.display(); // Output: Car Model: sedan, Color: blue
}
In the above code, we created two Car
objects named car1
and car2
. Each object stores the color and model information provided at creation, and we can output each car’s information by calling the display
method.
4. Various Components of a Class
A class can include various components. These include constructors, methods, fields, and access modifiers. Let’s take a detailed look.
4.1 Constructor
A constructor is called when an object is created and is responsible for initializing the object. Dart (the programming language for Flutter) supports named constructors in addition to the default constructor:
class Person {
String name;
int age;
Person(this.name, this.age); // Default constructor
Person.named(this.name, this.age); // Named constructor
}
The named constructor provides different ways to initialize, for example, it can be used like Person.named('John Doe', 30)
.
4.2 Method
A method is a function defined within a class. It defines the behavior the object will perform. Methods can change the state of the class or perform operations:
class Animal {
String name;
Animal(this.name);
void speak() {
print('$name is making a sound.');
}
}
4.3 Field
A field refers to the data variable that belongs to a class. It is used to maintain the state of the object. Fields can be categorized into instance variables and static variables:
class Circle {
static const double pi = 3.14; // Static variable
double radius; // Instance variable
Circle(this.radius);
}
4.4 Access Modifier
Dart allows setting access restrictions on a class’s fields and methods using various access modifiers, primarily the concepts of public
and private
. For example, prefixing a field with _
makes it private:
class BankAccount {
double _balance; // Private variable
BankAccount(this._balance);
void deposit(double amount) {
_balance += amount;
}
double get balance => _balance; // Public method
}
5. Class Inheritance
Inheritance is the ability to create new classes based on existing ones. This makes code reuse and structural hierarchy easier. Here’s an example of class inheritance:
class Vehicle {
void start() {
print('Vehicle started');
}
}
class Bike extends Vehicle {
void ringBell() {
print('Bicycle bell sound!');
}
}
In the above example, the Bike
class extends the Vehicle
class, meaning it can use the method start
from the Vehicle
class:
void main() {
Bike bike = Bike();
bike.start(); // Output: Vehicle started
bike.ringBell(); // Output: Bicycle bell sound!
}
6. Polymorphism
Polymorphism refers to the ability of objects of different classes to invoke the same method. This enhances the flexibility and reusability of the code. For example:
class Shape {
void draw() {
print('Drawing a shape.');
}
}
class Circle extends Shape {
@override
void draw() {
print('Drawing a circle.');
}
}
class Square extends Shape {
@override
void draw() {
print('Drawing a square.');
}
}
Here, various shapes like circles and squares inherit the Shape
class and override the draw
method to display appropriate results for each shape.
Conclusion
In this course, we have examined the concepts of classes, objects, and instances in Flutter in detail. Object-Oriented Programming is an important concept that serves as the foundation for app development, making it very useful for understanding and designing the structure of Flutter applications. In future courses, we will continue to explore how to write practical applications using these object-oriented concepts.
I hope this article is helpful for your Flutter learning journey!