Paramclasses Academy - Blog

Tutorials Details

←
Post 8 of 40
→

Dart Object-Oriented Programming: Classes, Objects and Constructors | Param Classes

Dart Object-Oriented Programming: Classes, Objects and Constructors | Param Classes

November 2, 2026

Dart Object-Oriented Programming: Classes, Objects and Constructors

❖ Welcome to Param Classes!

Welcome to Param Classes! In this tutorial, we will learn Object-Oriented Programming (OOP) in Dart. OOP is an important concept in programming and is widely used in Dart and Flutter app development.

When you start developing Flutter applications, you will work with classes, objects, properties, methods and constructors regularly. Flutter itself uses classes to create widgets and manage application functionality.

If you are a beginner, don't worry. We will understand each concept step by step with simple Dart code examples.

✦ 1. What Is Object-Oriented Programming (OOP)?

Object-Oriented Programming is a programming approach in which we organize code using classes and objects.

For example, think about a student management application. Every student may have:

  • Student Name

  • Roll Number

  • Course

  • Mobile Number

  • Student Details

Instead of writing separate variables for every student, we can create one Student class and use it to create multiple student objects.

OOP helps us:

  • Organize code properly.

  • Reuse code.

  • Manage large applications.

  • Make programs easier to maintain.

  • Represent real-world objects in programming.

✦ 2. What Is a Class in Dart?

A class is a blueprint or template used to create objects.

For example, a Student class can define the common information and actions that every student will have.

Example:

class Student {
  String name = "Rahul";
  int rollNo = 101;

  void displayDetails() {
    print("Name: $name");
    print("Roll Number: $rollNo");
  }
}

In this example:

  • class Student creates a class named Student.

  • name is a property.

  • rollNo is another property.

  • displayDetails() is a method.

  • print() displays the information.

A class only defines the structure. To use it, we create an object.

✦ 3. What Is an Object in Dart?

An object is an instance of a class.

We can create multiple objects from one class.

Example:

class Student {
  String name = "Rahul";
  int rollNo = 101;

  void displayDetails() {
    print("Name: $name");
    print("Roll Number: $rollNo");
  }
}

void main() {
  Student student1 = Student();

  student1.displayDetails();
}

Output:

Name: Rahul
Roll Number: 101

Here, student1 is an object of the Student class.

We can also create another object:

Student student1 = Student();
Student student2 = Student();

Both objects are created from the same class.

✦ 4. Understanding Properties and Methods

A class generally contains two important things:

1. Properties: Variables that store information about an object.

2. Methods: Functions defined inside a class that perform specific tasks.

Example:

class Car {
  String brand = "Toyota";
  String color = "Black";

  void start() {
    print("Car Started");
  }

  void stop() {
    print("Car Stopped");
  }
}

void main() {
  Car myCar = Car();

  print(myCar.brand);
  print(myCar.color);

  myCar.start();
  myCar.stop();
}

Output:

Toyota
Black
Car Started
Car Stopped

In this example, brand and color are properties, while start() and stop() are methods.

✦ 5. What Is a Constructor in Dart?

A constructor is a special function that runs automatically when an object is created.

Constructors are mainly used to initialize object properties.

Let's understand with an example.

class Student {
  String name;
  int rollNo;

  Student(this.name, this.rollNo);

  void displayDetails() {
    print("Name: $name");
    print("Roll Number: $rollNo");
  }
}

void main() {
  Student student1 = Student("Rahul", 101);
  Student student2 = Student("Priya", 102);

  student1.displayDetails();
  student2.displayDetails();
}

Output:

Name: Rahul
Roll Number: 101

Name: Priya
Roll Number: 102

Here:

  • Student() is the constructor.

  • this.name assigns the received value to the object's name property.

  • this.rollNo assigns the received value to the object's roll number property.

  • Each object can store different information.

✦ 6. Types of Constructors in Dart

Dart supports different ways to create and initialize objects.

❖ A. Default Constructor

When you don't define a constructor, Dart provides an implicit default constructor if the class allows it.

class Student {
  String name = "Rahul";
  int rollNo = 101;
}

void main() {
  Student student = Student();

  print(student.name);
  print(student.rollNo);
}

❖ B. Parameterized Constructor

A parameterized constructor accepts values when an object is created.

class Employee {
  String name;
  double salary;

  Employee(this.name, this.salary);

  void showDetails() {
    print("Name: $name");
    print("Salary: $salary");
  }
}

void main() {
  Employee emp = Employee("Amit", 25000);

  emp.showDetails();
}

❖ C. Named Constructor

A named constructor gives another way to create objects with different initialization options.

class Student {
  String name;
  int rollNo;

  Student(this.name, this.rollNo);

  Student.guest()
      : name = "Guest Student",
        rollNo = 0;

  void display() {
    print("$name - $rollNo");
  }
}

void main() {
  Student student1 = Student("Rahul", 101);
  Student student2 = Student.guest();

  student1.display();
  student2.display();
}

Output:

Rahul - 101
Guest Student - 0

✦ 7. What Is the this Keyword in Dart?

The this keyword refers to the current object.

It is commonly used when constructor parameters and class properties have the same names.

Example:

class Mobile {
  String brand;

  Mobile(this.brand);

  void showBrand() {
    print("Mobile Brand: ${this.brand}");
  }
}

void main() {
  Mobile phone = Mobile("Samsung");

  phone.showBrand();
}

Output:

Mobile Brand: Samsung

Here, this.brand refers to the brand property of the current object.

✦ 8. What Is Encapsulation in Dart?

Encapsulation means keeping data and related methods together inside a class and controlling how that data is accessed.

In Dart, a name beginning with an underscore is private to its library.

Example:

class BankAccount {
  double _balance = 5000;

  double getBalance() {
    return _balance;
  }

  void deposit(double amount) {
    if (amount > 0) {
      _balance += amount;
    }
  }
}

void main() {
  BankAccount account = BankAccount();

  account.deposit(1000);

  print("Balance: ${account.getBalance()}");
}

Output:

Balance: 6000.0

Here, _balance is private to its library. The deposit() and getBalance() methods provide controlled access to it.

✦ 9. Practical Example: Student Management Using OOP

Now let's create a simple student management example using classes, objects, constructors and methods.

class Student {
  String name;
  int rollNo;
  String course;

  Student(this.name, this.rollNo, this.course);

  void displayDetails() {
    print("--------------------");
    print("Student Name: $name");
    print("Roll Number: $rollNo");
    print("Course: $course");
    print("--------------------");
  }
}

void main() {
  Student student1 =
      Student("Rahul", 101, "Flutter");

  Student student2 =
      Student("Priya", 102, "Dart");

  student1.displayDetails();
  student2.displayDetails();
}

Output:

--------------------
Student Name: Rahul
Roll Number: 101
Course: Flutter
--------------------

--------------------
Student Name: Priya
Roll Number: 102
Course: Dart
--------------------

❖ What Did We Learn from This Project?

  • Created a Student class.

  • Added three properties.

  • Used a parameterized constructor.

  • Created two student objects.

  • Used a method to display student information.

This is a simple example of how OOP can organize information in a real application.

✦ 10. Why Is OOP Important in Flutter Development?

Flutter uses Dart, and its application structure is strongly based on classes and objects.

For example, when you create a Flutter screen, you commonly write:

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(
        child: Text("Welcome to Flutter"),
      ),
    );
  }
}

Here:

  • HomePage is a class.

  • StatelessWidget is the parent class.

  • extends is used for inheritance.

  • build() is a method.

  • Scaffold, Center and Text are widget objects created through constructors.

Understanding OOP makes Flutter code easier to read, write and manage.

✦ 11. Practice Tasks for Beginners

Try these exercises yourself:

  1. Create a Car class with brand, model and price properties.

  2. Create two car objects with different values.

  3. Create an Employee class with name and salary.

  4. Use a parameterized constructor to initialize employee details.

  5. Create a BankAccount class with a private balance and deposit method.

Practice these programs in DartPad or your local Dart development environment.

Useful Learning Resources:

❖ Conclusion

Object-Oriented Programming is one of the most important concepts in Dart programming. In this tutorial, we learned about classes, objects, properties, methods, constructors, the this keyword and encapsulation.

At Param Classes, our goal is to help beginners understand programming through practical examples and step-by-step learning. Before moving to advanced Flutter development, practice creating your own classes and objects.

Keep Learning with Param Classes — Learn Today, Grow Tomorrow!

←
Post 8 of 40
→