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