Dart Classes
Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation.
A class can be defined as a prototype or blueprint of the object. It is a collection of all the similar types of objects. Class exhibits two main object-oriented programming features : data abstraction and data encapsulation.
- Data Abstraction – It is the method by which only the essential details are displayed.
- Data Encapsulation – It is the method of wrapping up of data and its behavior as a single unit.
Classes are the best example of data encapsulation as it wraps up the data members and member functions as a single unit, i.e., class. The actual internal working of the function is hidden from the user and hence data abstraction is also exhibited.
These members of the class can be accessed using the object of the class. After defining the class, we can create objects or instance of that class.
Syntax of Dart classes:
class class_name
{
< fields >
< getters / setters >
< constructors >
< functions >
}
Here, class_name refers to the name of the class and the definition of the class lies between the curly braces ‘{ }’. It can contain following things in class :
- Fields – Fields refer to the variables declared in the class. They represent the data related to the objects.
- Getters and setters are used to initialize and retrieve the values of the fields of a class. By default, every class has a getter and setter however, we can override them explicitly.
- Constructors or destructors – Constructors are used to allocate the memory to the objects of a class, while the destructors are sued to deallocate the memory when the objects are no more in scope.
- Functions – Functions are the behavior of the object. Often referred to as methods, they contain the definition of actions to be performed on object.
Creating the object of the class
After defining the class, its object can be created to access the data members or member functions of that class. In Dart, an object of that class can be created using the ‘ new ’ keyword followed by the class_name.
Syntax of creating object :
var object_name = new class_name( < constructor_arguments > ) ;
Here, object_ name refers to the name that you assign to that object, class_name refers to the name of the class. Class_name ( < constructor_arguments > ) invokes the constructor. Some values must be passed in the < constructor_arguments > part if the constructor is parameterized.
Example :
var s1 = new student( ) ;
var s2 = new student( 5 ) ;
Accessing Instance Variable and Functions of the class
To access the members of a class, dot operator (.) is used along with the object_name.
Syntax :
object.data_member ;
object.member_function( ) ;
Program
// definition of the class students
class Students
{
// data members of the class
var sName = ' Yukta ' ;
var sAge = 19 ;
var sCourse = ' BCA ' ;
// member function of the class
display( )
{
print( " Name of the students is : ${ sName } " ) ;
print( " Age of the student is : ${ sAge } " ) ;
print( " Course student is enrolled in : ${ sCourse } " ) ;
}
}
void main( )
{
var s = new Students( ) ; // declaring the object of the class students
print( ' \n Details of the student is : ' ) ;
s.display( ) ; // accessing the display( ) function of the class using its object
}
Output :
Details of the student is :
Name of the students is : Yukta
Age of the student is : 19
Course student is enrolled in : BCA
Getters and Setters in Dart
Getter and setter methods are the methods that manipulate the data of the class fields. They are the methods that are used to read or write the properties of an object. Getter is used to read or get the data of the class field whereas setter is used to write or set the data of the class field to some variable.
Getter Method in Dart
It is used to retrieve a particular class field and save it in a variable. All classes have a default getter method but it can be overridden explicitly.
Consider the syntax of creating a getter in the class :
Syntax :
return_type get field_name {
. . .
}
Setter Method in Dart
It is used to set the data inside a variable received from the getter method. All classes have a default setter method but it can be overridden explicitly.
Consider the syntax of creating a setter in the class :
Syntax :
set field_name {
...
}
Program
Consider the following code in Dart that explains the concept of Getter and Setter in Dart :
// defining a class named student
class student {
// initializing sName property
String sName = ' null ' ;
/* creating a getter method getName
* to input value of sName from the user */
String get getName {
// returning the value of sName
return sName ;
}
/* creating a setter method setName
* to set the value of sName */
set setName( String name ) {
sName = name ;
}
}
void main( ) {
// initializing the object s1 of the class student
student s1 = student( ) ;
// Calling the set_name method(setter method we created)
// To set the value in Property " This is the tutorial for classes ! "
s1.setName = " This is the tutorial for classes ! " ;
// Calling the get_name method(getter method we created)
// To get the value from Property " sName ”
print( " Hey, ${ s1.getName } " ) ;
}
Output
Hey, This is the tutorial for classes !
Instance and Class Methods in Dart
We can create methods/functions in the Dart classes to perform specific actions. They help reduce the complexity of the code by avoiding writing the same lines of code repeatedly. As we already know, like the Dart functions, methods of classes may and may not return any value, and also, they may or may not take any parameter as input.
There are mainly two types of methods in Dart:
- Instance Method / Object Method
- Class Method
Instance Method in Dart :
These are the normal instance methods of the class that are used to access instance variables. By default, the member functions of the class are instance methods unless declared static. To use this method in the class, you must first create an object.
Syntax :
// Declaring an instance method
return_type method_name( )
{
// Body of this method
}
// Creating an object of the class
class_name object_name = new class_name( ) ;
// Calling instance method
object_name.method_name( ) ;
Creating instance method in Dart :
// Creating a class 'add’
class add {
// Initializing instance variables a and b
int a = 0 ;
int b = 0 ;
/* Creating an instance method with two
* arguments and a return type of integer */
void sum( int c, int d )
{
/* Using this keyword to set the values
* of the input to instance variable */
this.a = c ;
this.b = d ;
// printing the sum of two numbers
print( ' Sum of numbers is ${ a + b } ' ) ;
}
}
void main( )
{
// Creating an object ' a1 ' of the class ' add '
add a1 = new add( ) ;
// Calling the method sum with the use of object
a1.sum( 5, 4 ) ;
}
Output :
Sum of numbers is 9
Class Method in Dart :
Other than instance methods, there is another type of member function known as the class method. These class methods can be created using the 'static' keyword. An important point to remember here is that static member functions of the class can only access state data members. They can't access or invoke non-static members of the class.
The static member functions of the class can be invoked or called without using the object of the class.
Syntax :
// Creating class method
static return_type method_name( ) {
// Body of method
}
// Calling class method
class_name.method_name( ) ;
Creating class method in Dart –
// Creating the class named ' add '
class add
{
/* defining the static member function
* 'sum' of this class with void as
* return type and two integer arguments */
static void sum( int c, int d )
{
// Printing the sum of these two numbers
print( ' Sum of numbers is ${ c + d } ' ) ;
}
}
void main( )
{
/* invoking the ' sum ' function of the class
* without using any object */
add.sum( 5, 4 ) ;
}
Output :
Sum of numbers is 9
Constructors
Constructors are the special functions of classes that are used to initialize the objects when created in the program. Every class has one default constructor which is automatically called whenever an object is created. However, we can define the constructor explicitly also. They have the same name as the class name and do not have any return type.
Syntax :
class_name( < paremeters > )
{
// body of the constructor
}
Here, the ‘class_name’ refers to the name of the class whose constructor has to be created. The ‘parameters’ refer to the arguments passed to the constructor. However, it is optional.
The body of the constructor contains sever lines of code that explains the functionality of the constructor.
Following are the types of constructors in Dart classes :
- Default Constructor
- Parameterized Constructor
- Named Constructor
Consider the following code in Dart that explains the concept of constructors
// Defining the class student
class student
{
/* defining the constructor of the class student
* with the same name as of the class, and no r
* return type */
student( )
{
/* Everytime the constructor is invoked, this
* line is printed */
print( ' Constructor is being invoked ! ' ) ;
}
// initialzing a string str
String str = ' NULL ' ;
// Creating Function inside class
void display(){
print( " The string is $str " ) ;
}
}
void main( )
{
// Creating an object s1 of the class student
student s1 = new student();
/* Accessing the instance variable str using
* the object and intializing it with some
* value */
s1.str = ' This tutorial explains constructor also ! ' ;
/* Accessing the instance function display using
* the object to display the value of string */
s1.display();
}
Output
Constructor is being invoked !
The string is This tutorial explains constructor also !
As you can see, before any string the line inside the constructor is printed even though it is no where called. This is so because, the moment we create an object of the class, the compiler is automatically invoked. In this case, the first line in main( ) was the object creation, therefore this line got printed first.
Destructors
Destructors are the special functions in the class that are used to de-allocate the memory assigned to the objects as soon as they reach out of their scope. However, we don't need to actively delete objects in Dart. Dart is a garbage-collected language, so any object that we don't hold any references to will eventually be garbage collected and freed by the runtime system.
Advantages of Objects
There are various benefits of using object-oriented programming. Below are the few benefits :
- Modularity - The source code of an object can be maintained individually and can hide from the other object's source code.
- Data – hiding - Using oops programming, the details of the internal functionality of code are hidden from the others. For example - Users only interact with the application, but they don't familiar with the internal implementation.
- Reusability - We don't need to write the same code again and again. We can use the object of class multiple times in our program.
- Pluggability and debugging easy - If any object is creating a problem in our program, and then we can replace it in our program and plug the new object as its replacement. The oops code can be easy to debug.