Object-Oriented

Classes & Object

Dart classes, often known as object constructors, are the object’s blueprint. A class may have constructors, fields, functions, and other elements. It is a wrapper that, when an object is created, binds or encapsulates the functions and data together. A class is known as a user-definable data type since all of its objects determine its characteristics.

A class could be thought of as a prototype or sketch. It includes all detail including the model name, year, features, cost, and so forth. We are able to construct the automobile using these attributes. Here, the vehicle is an item. In order to access all the properties, we can build many objects representing different types of cars.

Defining a Class in Dart

To define a class in Dart, use the class keyword followed by the class name; all fields and functions are surrounded in a pair of curly brackets ({}). Below is the syntax.

Syntax

				
					class ClassName {  
   <fields>  
   <getters/setters>  
  <constructor>  
 <functions>  
}  
				
			

In this case, the user-defined class’s actual name is represented by the ClassName. We provide a class definition in curly brackets. Fields, constructors, getters, setters, and methods can all be found in a class.

Note: The class name’s initial letter must be capitalized and contain no separators in accordance with the naming convention requirement for identifiers.

Example

				
					void main() {   
   // Defining class  
 class Student {  
   var stdName;  
   var stdAge;  
   var stdRoll_nu;  
     
   // Class Function  
    showStdInfo() {  
        print("Student Name is : ${stdName}");  
        print("Student Age is : ${stdAge}");  
        print("Student Roll Number is : ${stdRoll_nu}")  
}  
				
			

We created a class named Student in the class example above. The three fields in this class are stdName, stdAge, and stdRoll_nu. A class function that prints the class fields is called showStdInfo(). We must create the class’s object in order to access its properties.

Dart Object

Programming in Dart is object-oriented; everything is handled as an object. A variable or instance of a class that is used to access the properties of the class is called an object. State and behavior are two characteristics of an object. Let’s say a man is an item with a name, age, and level of health as well as a behavior (running, walking, and sleeping). Programming objects have states and behaviors, much like real-world things do in theory. A class, or template, is used to construct an object.

While the method reflects an object’s behavior, the fields of the classes are saved as object states.
COMMERCIAL

Creating Class Objects in Dart

We can build an instance or object of the class after it has been created in order to access its properties and functionalities. To declare a class, use the new keyword and then the class name. Below is the general syntax for generating an object of a class.

Syntax

				
					var object_name  = new class_name(<constructor_arguments>);  
				
			

In this case, object_name and class_name stand for the respective real object name and class name. Constructor parameters need to be supplied a value if the class constructor is parameterized.

Example

				
					   // Defining class  
 class Student {  
   var stdName;  
   var stdAge;  
   var stdRoll_nu;  
     
   // Class Function  
    showStdInfo() {  
        print("Student Name is : ${stdName}");  
        print("Student Age is : ${stdAge}");  
        print("Student Roll Number is : ${stdRoll_nu}")  
  
}  
}  
void main () {  
 // Creating Object called std  
  var std = new Student();  
}  
				
			

Although the object known as std of the class Student has been constructed, this is insufficient. The newly formed object must be used in order to access the properties.

Assessing Instance Variable and Function

We may access the class’s fields and functions after generating an object. The instance name and the class property name are separated by the (.) operator. Below is the syntax.

Syntax

				
					objectName.propName or objectName.methoName()  
				
			

Example

				
					// Defining class  
 class Student {  
   var stdName;  
   var stdAge;  
   var stdRoll_nu;  
     
   // defining class function  
    showStdInfo() {  
        print("Student Name is : ${stdName}");  
        print("Student Age is : ${stdAge}");  
        print("Student Roll Number is : ${stdRoll_nu}");  
  
               }  
}  
void main () {  
  
  // Creating object called std  
  var std = new Student();  
  std.stdName = "Peter";  
  std.stdAge =24;  
  std.stdRoll_nu = 90001;  
// Accessing class Function  
 std.showStdInfo();  
}  
				
			

Output

				
					Student Name is: Peter
Student Age is: 24
Student Roll Number is: 90001
				
			

Explanation

The student name, age, roll number, and showStdInfo() function were the only elements of the Student class that we constructed for the example above.

Next, we used the (.) operator to assign the values to each field in the Student class object that we had built. The information was output to the screen when we used the showStdInfo() function.

Benefit of Objects

The use of object-oriented programming has many advantages. The few advantages are listed below.

1.Modularity: An object’s source code can be hidden from the source code of other objects and maintained separately.

2.Data – hiding: Internal workings of code are concealed from the public through the use of oops programming. As an illustration, users don’t know anything about the internal implementation; they just interact with the program.

3.Reusability: We can avoid writing the same code repeatedly. The class object may be used more than once in our software.

4.Pluggability and ease of debugging – If one object is causing issues for our application, we can simply plug in a new object to replace the problematic item. Debugging the oops code might be simple.

Share this Doc

Classes & Object

Or copy link

Explore Topic