Classes and Objects

  • Classes and Objects

Classes and Dart Objects

A class is a blueprint for creating objects. A class encapsulates data for the object. Dart is an object-oriented programming language. Everything in Dart is an object. Objects are instances of classes.

Variables in class are known as properties when declared inside a class. Varibales are used to define the state of the class.

Functions when declared inside a class are called methods. Functions (methods) are used to modify the variables (state/properties) of the class.

Familiar with the topic? Play Quiz

Declaring a Class

To declare the classes in dart we use the class keyword with class name followed by the curly brackets:

Syntax

class ClassName {
  // class members
}

Here is an example of a class in Dart, We are declaring the class with the name Person having the name and age properties.

Example

class Person {
  String name = "Jamie";
  int age = 30;
}

Creating an Object

To create objects of dart class

ClassName objectName = ClassName();

Here is an example of creating an object in Dart:

Person person = Person();

Accessing Class Members

You can access class members using the dot (.) operator. Here is an example of accessing class members in Dart:



print(person.name);
print(person.age);

Output

Jamie
30

code Try Yourself

Cascade Operators

You can access more than two properties or methods using cascade operators. It is denoted by (..).

person
..name
..age; 

Class variables and methods

In class there are two types of variables static and non-static varibales in class. Let’s understand the difference between them.

Non-static Variables and Methods

Non-static variables

Non static variables are the normal variables that store values and can only be accessed with the object of class. These kind of variables are known as class properties.

Example

class MyClass {

  int age = 20; 
}

Accessing the non-static variable

MyClass myObject = MyClass(); 
print(myObject.age);

Non-static methods

Non static methods are the normal functions that does something and gives the result out from it. Non-static methods can only be accessed with the help of class object.

Example

class MyClass {

  int calculateAge() { 
    int age = 10; 

    return age; 
  }
}

Accessing the non-static methods variable

MyClass myObject = MyClass(); 
print(myObject.calculateAge());

Static Variables and Methods

Static variables

Static variables are the normal variables with a static keyword,that store values and can be accessed with the name of the class. These kind of variables are known as class static properties.

Example

class MyClass {

  static int age = 20; 
}

Accessing the static variable

print(MyClass.age);

Static Methods

Static methods are the normal functions that does something and gives the result out from it with a static keyword before return type. Static methods can only be accessed with the help of class name.

Example

class MyClass {

 static int calculateAge() { 
    int age = 10; 

    return age; 
  }
}

Accessing the static methods variable


print(MyClass.calculateAge());

Quiz