{"id":19829,"date":"2022-05-29T18:50:56","date_gmt":"2022-05-29T18:50:56","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3902"},"modified":"2022-05-29T18:50:56","modified_gmt":"2022-05-29T18:50:56","slug":"dart-classes-and-object","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/29\/dart-classes-and-object\/","title":{"rendered":"Dart Classes and Object"},"content":{"rendered":"\n<p>Dart classes are the blueprint of the object, or it can be called object constructors. A class can contain fields, functions, constructors, etc. It is a wrapper that binds\/encapsulates the data and functions together; that can be accessed by creating an object. A class can refer to as user-define data type which defines characteristics by its all objects.<\/p>\n\n\n\n<p>We can assume a class as a sketch (prototype) or a car. It contains all the details about model name, year, features, price, etc. Based on these properties of the car, we can build the car. Here the car is an object. There can be many cars so we can create many objects of cars to access all the properties.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Defining a Class in Dart<\/h2>\n\n\n\n<p>Dart\u00a0provides\u00a0<strong>class<\/strong>\u00a0keyword followed by a class name is used to define a class; all fields and functions are enclosed by the pair of curly braces ({}). The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ClassName {  \n   &lt;fields>  \n   &lt;getters\/setters>  \n  &lt;constructor>  \n &lt;functions>  \n}  <\/code><\/pre>\n\n\n\n<p>Here, the ClassName represents the actual name of the class, which is defined by the user. In curly braces, we provide a class definition. A class can consist of fields, constructors, getters setters, and methods.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Note &#8211; According to the naming convention rule of identifiers, the first letter of the class name must be capital and use no separators.<\/h4>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><\/p>\n\n\n\n<ol><li><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {   \n   \/\/ Defining class  \n class Student {  \n   var stdName;  \n   var stdAge;  \n   var stdRoll_nu;  \n     \n   \/\/ Class Function  \n    showStdInfo() {  \n        print(\"Student Name is : ${stdName}\");  \n        print(\"Student Age is : ${stdAge}\");  \n        print(\"Student Roll Number is : ${stdRoll_nu}\")  \n}  <\/code><\/pre>\n\n\n\n<p>In the above example of class, we declared a class called&nbsp;<strong>Student<\/strong>. This class has three fields&nbsp;<strong>stdName, stdAge<\/strong>, and&nbsp;<strong>stdRoll_nu<\/strong>. The&nbsp;<strong>showStdInfo()<\/strong>&nbsp;is a class function which prints the fields of class. To access the properties of the class, we need to create its object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dart Object<\/h2>\n\n\n\n<p>Dart is object-oriented programming, and everything is treated as an object in Dart. An object is a variable or instance of the class used to access the class&#8217;s properties. Objects have two features &#8211; state and behavior. Suppose a man is an object with a state (name, age, health) and behavior (walking, running, and sleeping). Programming objects are theoretically similar to the real-life objects; they also have state and behavior. An object is created from a template which is known as class.<\/p>\n\n\n\n<p>The fields of the classes are stored as object states, whereas the method represents an object&#8217;s behavior.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Class Objects in Dart<\/h2>\n\n\n\n<p>After creating the class, we can create an instance or object of that class which we want to access its fields and functions. The&nbsp;<strong>new<\/strong>&nbsp;keyword is used to declare class followed by the class name. The general syntax of creating an object of a class is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><\/p>\n\n\n\n<ol><li>var&nbsp;object_name&nbsp;&nbsp;=&nbsp;<strong>new<\/strong>&nbsp;class_name(&lt;constructor_arguments&gt;);&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>Here, object_name and class_name signifies as the actual object name and class name respectively. If the class constructor is parameterized then constructor arguments must be passed value.<\/p>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \/\/ Defining class  \n class Student {  \n   var stdName;  \n   var stdAge;  \n   var stdRoll_nu;  \n     \n   \/\/ Class Function  \n    showStdInfo() {  \n        print(\"Student Name is : ${stdName}\");  \n        print(\"Student Age is : ${stdAge}\");  \n        print(\"Student Roll Number is : ${stdRoll_nu}\")  \n  \n}  \n}  \nvoid main () {  \n \/\/ Creating Object called std  \n  var std = new Student();  \n}  <\/code><\/pre>\n\n\n\n<p>We have created the object called&nbsp;<strong>std<\/strong>&nbsp;of the class&nbsp;<strong>Student<\/strong>&nbsp;but only creating an object not enough. We have to access the properties by using the newly created object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Assessing Instance Variable and Function<\/h2>\n\n\n\n<p>After creating an object, we can access the fields and methods of the class. The class property name is separated by the (.) operator with the instance name. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-classes-and-object#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Defining class  \n class Student {  \n   var stdName;  \n   var stdAge;  \n   var stdRoll_nu;  \n     \n   \/\/ Class Function  \n    showStdInfo() {  \n        print(\"Student Name is : ${stdName}\");  \n        print(\"Student Age is : ${stdAge}\");  \n        print(\"Student Roll Number is : ${stdRoll_nu}\")  \n  \n}  \n}  \nvoid main () {  \n \/\/ Creating Object called std  \n  var std = new Student();  \n}  <\/code><\/pre>\n\n\n\n<ol><li>objectName.propName&nbsp;or&nbsp;objectName.methoName()&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Defining class  \n class Student {  \n   var stdName;  \n   var stdAge;  \n   var stdRoll_nu;  \n     \n   \/\/ defining class function  \n    showStdInfo() {  \n        print(\"Student Name is : ${stdName}\");  \n        print(\"Student Age is : ${stdAge}\");  \n        print(\"Student Roll Number is : ${stdRoll_nu}\");  \n  \n               }  \n}  \nvoid main () {  \n  \n  \/\/ Creating object called std  \n  var std = new Student();  \n  std.stdName = \"Peter\";  \n  std.stdAge =24;  \n  std.stdRoll_nu = 90001;  \n\/\/ Accessing class Function  \n std.showStdInfo();  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Student Name is: Peter\nStudent Age is: 24\nStudent Roll Number is: 90001\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>In the above example, we created a class called&nbsp;<strong>Student<\/strong>, which consisted of the student name, age, roll number, and&nbsp;<strong>showStdInfo()<\/strong>&nbsp;function to show the student details.<\/p>\n\n\n\n<p>Then, we created a Student class object and assigned the values to each field by using the (.) operator. We called the showStdInfo() function that displayed the details as an output to screen.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefit of Objects<\/h3>\n\n\n\n<p>There are various benefits of using object-oriented programming. Below are the few benefits.<\/p>\n\n\n\n<ol><li><strong>Modularity:<\/strong>&nbsp;The source code of an object can be maintained individually and can hide from the other object&#8217;s source code.<\/li><li><strong>Data &#8211; hiding:<\/strong>&nbsp;Using oops programming, the details of the internal functionality of code are hidden from the others. For example &#8211; Users only interact with the application, but they don&#8217;t familiar with the internal implementation.<\/li><li><strong>Reusability &#8211;<\/strong>&nbsp;We don&#8217;t need to write the same code again and again. We can use the object of class multiple times in our program.<\/li><li><strong>Pluggability and debugging easy &#8211;<\/strong>&nbsp;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.<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Dart classes are the blueprint of the object, or it can be called object constructors. A class can contain fields, functions, constructors, etc. It is a wrapper that binds\/encapsulates the data and functions together; that can be accessed by creating an object. A class can refer to as user-define data type which defines characteristics by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[266],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/19829"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=19829"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/19829\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=19829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=19829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=19829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}