×

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

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 Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Dart Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

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

Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 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 HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 minutes read.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

2 minutes read.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 minutes read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Dart Packages

Every programming language has some in-built functions stored inside the header files that makes the task much easier for the programmer. The Dart Packages refer to the compilation of a...

4 minutes read.

Dart Arithmetic Operators

Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc. They are binary operators that work upon two operands. Consider A...

2 minutes read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

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.

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 Enumerations

Enumeration data type in Dart In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.  Implementation =  enum enum_name {    ...

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

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.