Table of contents
1.
Introduction
2.
Dart Classes
2.1.
Syntax of Class Declaration
2.2.
Example Program
3.
Dart Objects
3.1.
Syntax of creating Objects
3.2.
Example Program
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dart Classes and Objects

Author Vasu Bansal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will discuss classes and objects in Dart. Like many other modern programming languages, Dart is also an object-oriented programming language that supports the usage of classes, objects, and many other object-oriented constructs. In the next section, we will study in-depth about classes in Dart.

Dart Classes

The class is a blueprint for creating objects in Dart. Classes are used to encapsulate data and the functions that perform operations on that data. A class can be thought to be a user-defined data type similar to the way there are primitive data types in Dart. 

Feel free to refer to the blog Dart Data Types on the Coding Ninjas Website to read more about the various dart types that Dart supports. 

Syntax of Class Declaration

The following is the syntax to declare a class in Dart. The keyword class is used to declare classes in Dart. The body of the class needs to be encapsulated between a pair of curly braces, i.e. {}. As discussed earlier, the body of the class contains the fields, the constructors and the relevant functions, including getters and setters that operate on these fields.

class your_class_name {  
   <fields> 
   <constructors> 
   <getters/setters>    
   <functions> 
}

Example Program

The following is a sample program that declares a class named Ninja with three fields and one function. The keyword class is used to declare the class. We will see how to create an object for this class in the next section.

Code:

class Ninja{
  String ninja_name = "John";
  String batch_name = "A1";
  int roll_no = 10;

  void fetch_ninja_details(){
    print("Ninja name: " + ninja_name);
    print("Batch name: " + batch_name);
    print("Roll no: ${roll_no}");
  }
}

Dart Objects

The objects are the actual instances of a class. They are used to access the properties and functions of a class. The objects in object-oriented programming have correspondence to real-life objects. For example, the species of human beings will match the notion of a class in OOPs, and each human body is an object of that class Human in the language of OOPs.

Syntax of creating Objects

The keyword new is used to create an instance of a class. For example, to create an instance of the Ninja class that we discussed in the previous section, use the following line of code.

Ninja obj = new Ninja();

Example Program

Recall that we had earlier seen a program where we declared a class named Ninja with some fields and functions in it. Now in this program, we will see how to create an object of this class and call the functions of this class with the help of that object.

Code:

class Ninja{
  String ninja_name = "John";
  String batch_name = "A1";
  int roll_no = 10;

  void fetch_ninja_details(){
    print("Ninja name: " + ninja_name);
    print("Batch name: " + batch_name);
    print("Roll no: ${roll_no}");
  }
}

void main(){
  Ninja obj = new Ninja();
  obj.fetch_ninja_details();  
}

Output:

Ninja name: John
Batch name: A1
Roll no: 10  

 

Know What is Object in OOPs here in detail.

Frequently Asked Questions

  1. What are the advantages of using classes in your Dart program?
    There are many advantages of using classes; for example, it increases the code quality and readability, increases modularization, features of abstraction and inheritance, and many more.
     
  2. How to access a class's member functions and properties using objects in Dart?
    You can use the member functions and properties of a class using the dot operator.
     
  3. How does the use of classes promote the reusability of the code?
    The use of classes and objects promotes the reusability of the code as one can use the same function and properties at various other places in the program by creating a separate object of the same class.

Conclusion

In this article, we have extensively discussed Classes and Objects in Dart and saw sample programs containing their implementation in Dart. You can also read the blog Dart Constructors and Super Constructors on the Coding Ninjas Website to learn more about constructors in Dart.

We hope this blog has helped you enhance your knowledge regarding Dart Classes and Objects. If you want to learn more, check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass