Open In App

Dart - Classes And Objects

Last Updated : 02 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Dart is an object-oriented programming language, so it supports the concept of class, object, etc. In Dart, we can define classes and objects of our own. We use the class keyword to do so. Dart supports object-oriented programming features like classes and interfaces.

Let us learn about Dart Classes and Objects in this article.

Classes in Dart

Class is the blueprint of objects, and class is the collection of data members and data function means, which include these fields, getter and setter, and constructor and functions.

Declaring class in Dart

class class_name {
// Body of class
}

In the above syntax: 

  • class is the keyword used to initialize the class.
  • class_name is the name of the class.
  • The body of the class consists of fields, constructors, getter and setter methods, etc.

The body of a Constructor includes three things: Class Fields, Class Methods, and Constructors.

1. Class Fields in Dart

Classes Fields are the variables which data for the objects. Let us check with an Example:

class Student {

// Fields defining the
// Properties of Class
int? roll_no;
String? name;
}

2. Class Methods in Dart

Class Methods are the functions that provide behavior for an object. Let us check with an Example:

class Student {

// Fields defining the
// Properties of Class
int? roll_no;
String? name;

void print_name(){
print("Student Name: $name");
}
}

3. Constructors in Dart

A Constructor is a block of code that initializes the state and values during object creation. Constructor is name same as the class name and doesn't return any value.

class_name( [ parameters ] ) {
// Constructor Body
}

Objects in Dart

Objects are the instance of the class, and they are declared by using a new keyword followed by the class name (optional in Dart 2+).

Object Declaration

var object_name = new class_name([ arguments ]);

Or

var objectName = ClassName([ arguments ]);

In the above syntax:  

  • new is the keyword used to declare the instance of the class
  • object_name is the name of the object, and its naming is similar to the variable name in dart.
  • class_name is the name of the class whose instance variable is been created.
  • arguments are the inputs that are needed to be passed if we are willing to call a constructor.

After the object is created, there will be a need to access the fields that we will create. We use the dot(.) operator for that purpose.

Accessing Class Properties and Methods

// For accessing the property
object_name.property_name;

// For accessing the method
object_name.method_name();

Creating a Class and Accessing its Fields

Example 1 :

Dart
// Creating Class named Gfg
class Gfg {

    // Creating Field inside the class
    String geek1 = '';

    // Creating Function inside class
    void geek()
    {
        print("Welcome to $geek1");
    }
}

void main()
{
    // Creating Instance of class
    Gfg geek = new Gfg();

    // Calling field name geek1 and assigning value
    // to it using object of the class Gfg
    geek.geek1 = 'GeeksforGeeks';

    // Calling function name geek using object of the class Gfg
    geek.geek();
}


Output: 

Welcome to GeeksforGeeks

Explanation of the above Program: 

Here, we have first created the class named Gfg with a field geek1 and a function geek. Now, in the main function, we have created an object of the class Gfg of the name geek. Using this object, we have assigned the value GeeksforGeeks to the string variable of the class, and then we have called the geek function which has printed the output.

Example 2 :

Dart
class GFG { 
  
     String name = " "; 
  
     String get gfg1 { 
         return name; 
     } 
     void set gfg1(String name) { 
         this.name = name; 
     } 
     void result(){
         print(name);
    }
} 

void main() { 
   GFG gfg2 = new GFG(); 
   gfg2.name="GFG (GeeksforGeeks) is a well-known online platform that provides resources for learning programming, data structures, and algorithms.";
   gfg2.result(); 
}


Output: 

GFG (GeeksforGeeks) is a well-known online platform that provides resources for learning programming, data structures, and algorithms.

Dart's object-oriented programming (OOP) approach facilitates efficient code organization and promotes reusability. Understanding classes, objects, constructors, fields, and methods is essential for building scalable applications. By leveraging these concepts, developers can create robust applications while maintaining clean and modular code.


Next Article
Article Tags :

Similar Reads