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 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 a Example:

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

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

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.

Syntax:

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

Dart – Classes And Objects

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.

Similar Reads

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....

Objects in Dart

Objects are the instance of the class and they are declared by using a new keyword followed by the class name....

Creating a Class and Accessing its Fields

Example 1: Simple Example...

Contact Us