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.

Declaring class in Dart –


class class_name {

   // Body of class

In the above syntax:

  • Class is the keyword use to initialse the class.
  • class_name is the name of the class.
  • Body of class consists of fields, constructors, getter and setter methods, etc..

Declaring objects in Dart –

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


var object_name = new class_name([ arguments ]);

In the above syntax:

  • new is the keyword use to declare the instance of the class
  • object_name is the name of the object and its nameing is similar to variable name in dart.
  • class_name is the name of the class whose instance variable is been created.
  • arguments are the input which are needed to be pass if we are willing to call a constructor.

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


 // For accessing the property

// For accessing the method

Creating a class and accessing its fields –





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



Welcome to GeeksforGeeks

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

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Akanksha_Rai

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.