×

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects. 

Objects

Objects are the foundation of the Dart program, and it is a superclass that contains all the Dart classes except Null. 

An object represents the real-world entity. It contains two important components : data and methods, which are called members of an object. Objects use methods to send, process, and receive messages among each other.

Even numbers and string literals are objects in Dart.

Creating an object

An object of a class can be created using the following two steps :

1. Define a class

2. Instantiate an object of that class

Classes

A class is a collection of objects with similar states and behaviour. It is also called the blueprint of an object. Objects are called instances when they are created from the class at the runtime.

Implementation = A class can be created using the ‘ class ’ keyword.

                               class class_name { } 

Instantiating an object

A new instance of a particular class is created using a new keyword. Objects can have functions and data as its members; in order to refer them we use dot ( . ) operator.

Implementation = var b = class_name( ) ; 

As you can see, we skipped the keyword ‘ new ’ because according to Dart 2.0 the keyword ' new ' is optional. 

Otherwise, with new keyword syntax would be : 

                          var b = new class_name( ) ;

Note: All created objects implicitly inherit the " Object " class, which is the superclass. Therefore, even if an object is empty, it has its own inbuilt method and attributes.

Objects attributes in Dart

An attribute of an object defines its behaviour. They are also called instance variables or member fields of a class because they are variables defined in a class.

Every object of the class maintains a separate copy of the instance variable of that class.

Objects methods in Dart

A method is the function of the class that performs operations with the attribute of that class.

Program

class human { } // this is the declaration of an empty class


// defining a class
class square {
  // declaring an attribute named side
  var side ;
  
  // defining a method to calculate the area of the square
  int area( int r) 
  {
    int ar = r * r ;
    return ar ;
  }
}


void main( )
{
  // declaring an object s1 of the class
  var s1 = square( ) ;
  
  // initializing object's attribute side with value 4
  s1.side = 4 ;
  
  // calling the method area of the class. Variable a is used to hold the value returned by the function
  int a = s1.area( s1.side ) ;
  
  // printing the side of the square and it’s area using string interpolation
  print( " \n Side of the square is : ${s1.side} " ) ;
  print( " \n Area of the square is : $a " ) ;
}

Output

Side of the square is : 4 
Area of the square is : 16

Object Constructor in Dart

A constructor is a special kind of function with the same name as the class for which it is created. They do not have any return type but can accept parameters. They do not need any call statement because a constructor is automatically invoked when an object of a class is created. The use case of the instructor is to assign values to the objects of the class.

NOTE: When the object attributes and constructor arguments have the same name, we refer to the attributes using the 'this' keyword.

Named parameters in Dart

Named parameters are the parameters referenced by the name, allowing them to be used differently in the function invocation and declaration.

Implementation= to declare named parameters, we enclosed the parameters of the constructor inside the curly braces'{}'. One has to remember while passing values to the constructors, they have to be prefixed with the name of the parameters they are passed for.

Program

// declaring a class named numbers
class numbers {


  // initializing data members with values
  int a = 5 ; 
  int b = 10 ;


  // declaring a constructor with named parameters using this keyword
  numbers( this.a, this.b ) {


    // printing the values of a and b
    print( a ) ;
    print( b ) ;
  }
}


void main( ) {


  // declaring an object n1 of class numbers using new keyword
  var n1 = new numbers( 5, 6 ) ;
}

Output :

5
6

Related Topics

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes read.

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across...

3 minutes read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 minutes read.

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.

Dart Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

4 minutes read.

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.

Dart If statement

If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true....

1 minute read.

Dart Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 minutes read.

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.

Rust vs Dart

Rust is a system-level programming language that stands next to C ++ in terms of syntax, but offers greater speed and memory security. Dart, on the other hand, is an...

2 minutes read.

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 minutes read.

Dart Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.