The Class
is one of the object-oriented concepts in Dart language.
Since Dart is an object-oriented language, It provides each functionality and feature as classes and objects.
The class
is a blueprint of objects.
The class
contains the following components’ syntax.
class{
// instance member variables
constructor(){
}
// setters and getters
// functions and methods
}
How to create a class in Dart?
To create a class, use the class
keyword. It contains the following components:
- Class name : use
class
keyword to create a class - Constructor: It si called automatically during object creation, it is used to initialize the data of an object.
- Named constructor: It is used to create an object
- Instance member variables: It is used to store data of an object
- Setter and getter: It provides access mechanism to set and get data
- Functions or Methods : It is used to define the functionality
class Employee {
// instance member variable
String name = "john";
}
In the above example, the Employee class is created with a named variable containing a string.
How to create an object of a class
The object is a blueprint of a class.
The object can be created and used in other classes.
Declare a variable name for the type Class.
the object can be created using the optional new
keyword and optional class name for variable declaration.
The following is an example of object creation in multiple ways.
main() {
// use var and classname with ()
var employee = Employee();
// use var and new keyword and classname with ()
var employee1 = new Employee();
// use classname and new keyword and classname with ()
Employee employee2 = new Employee();
// use classname andclassname with ()
Employee employee3 = Employee();
print(employee);
print(employee.runtimeType);
}
class Employee {
String name = "john";
}
Dart Class constructor
The class contains a constructor
, which is a special function created with the same name as a class with or without arguments.
It is used to initialize the data of an object created from a class. That means, called when an object is created. Syntax:
class Class1 {
// constructor
Class1() { }
}
Notes:
- constructor is called automatically during object creation
- It is used to initialize the data of an object
- The name of the constructor and classname should match
- Optional Arguments, if arguments are provided, Object creation should be passed with arguments.
- The return type is not required
Here is a class constructor example
main() {
var employee = Employee("Andrew");
var employee1 = Employee("Mark");
print(employee.name);// Andrew
print(employee1.name); // Mark
}
class Employee {
String name = "john";
Employee(String name) {
this.name = name;
}
}
Constructors Contain arguments of different types
- Optional Arguments with or without parameter
The above example contains a parameterized constructor. If the constructor is not provided, Then it is called the default constructor.
Only one of the constructors from the default or parameter constructor must be defined.
- Named constructor
Dart does not allow multiple constructors of default or parameterized constructors.
It provided a named constructor with a name with passing arguments.
Here is an example parameterized constructor class in Dart.
main() {
var employee = Employee(); // default constructor called
var employee1 = Employee.changeNameTitle("Mark"); // Named constructor called
print(employee.name);
print(employee1.name);
}
class Employee {
String name = "john";
Employee() {}
Employee.changeNameTitle(String name) {
this.name = "Hello "+ name;
}
}
Dart class instance variables
The class contains the state of data represented in instance variable properties.
In the below example, the Class contains a name instance variable
class Employee {
String name = "john";
}
instance variables can be accessed using the object. instance variable.
main() {
// use var and classname with ()
var employee = Employee();
print(employee);
print(employee.name); //john
}
Dart class methods
Class contains methods, which are functions that are used to define the functionality of the class. It also modifies the state of the instance variables.
function definition in Dart contains
- return type
- function name
- arguments
- body
Syntax:
returntype functionName(arguments){
//body
}
Here is a class function example
In this example, the captilizeName
function capitalizes the first letter of a name.
main() {
var employee = Employee(); // default constructor called
var employee1 = Employee.changeNameTitle("mark"); // Named constructor called
print(employee.captilizeName());
print(employee1.captilizeName());
}
class Employee {
String name = "john";
Employee() {}
Employee.changeNameTitle(String name) {
this.name = "Hello " + name;
}
String captilizeName() {
return name[0].toUpperCase() + name.substring(1);
}
}
Dart class setter and getter method example
getter and setter are methods in a class used to read and modify the instance variables in Dart.
getter()
method reads the instance attribute of a class.
setter()
method updates the instance variable value to a new value.
- Setter and getter method syntax
Syntax
datatype get get[Variable]{
//return statement
}
set set[Variable](arguments){
//return statement
}
Here is an example setter and getter example in dart class
main() {
var employee = Employee(); // default constructor called
employee.setName = "john";
print(employee.getName);
}
class Employee {
String name = "john";
Employee() {}
set setName(String name) {
this.name = name;
}
String get getName {
return this.name;
}
}
There are some differences with the Java language.
getter has no braces()
in the declaration
To call a setter, you must assign the value
Conclusion
To summarize, Dart is an object-oriented language with class, constructors, instance variable, methods, getters and setters.