Dart: Classes

# Declaring a Class
class class_name {  
   <fields> 
   <getters/setters> 
   <constructors> 
   <functions> 
}

A class definition

FieldsA field is any variable declared in a class. Fields represent data pertaining to objects.
Setters and GettersAllows the program to initialize and retrieve the values of the fields of a class. A default getter/ setter is associated with every class. However, the default ones can be overridden by explicitly defining a setter/ getter.
ConstructorsResponsible for allocating memory for the objects of the class.
FunctionsFunctions represent actions an object can take. They are also at times referred to as methods.

Creating Instance of the class

var object_name = new class_name([ arguments ])

Accessing Attributes and Functions

//accessing an attribute 
obj.field_name  

//accessing a function 
obj.function_name()
# Dart Constructors
Class_name(parameter_list) { 
   //constructor body 
}

Named Constructors

Class_name.constructor_name(param_list)
# Dart Class ─ Getters and Setters

Syntax: Defining a getter

Return_type  get identifier 
{ 
} 

Syntax: Defining a setter

set identifier 
{ 
}
# Class Inheritance
class child_class_name extends parent_class_name 

Types of Inheritance

SingleEvery class can at the most extend from one parent class.
Multiple A class can inherit from multiple classes. Dart doesn’t support multiple inheritances.
Multi-levelA class can inherit from another child’s class.

Leave a Reply

Your email address will not be published. Required fields are marked *