Warning: Undefined array key "lang_code" in /home/pmfnldmy/public_html/androidatc/ap/extension/classes/common.php on line 800
Object-Oriented Programming (OOP) - Dart Programming

Object-Oriented Programming (OOP)

If you have never used an object-oriented programming language before, you’ll need to learn the basic concepts before you can start coding. This lesson will introduce you to objects, classes, inheritance, interfaces, and packages. Each discussion focuses on how these concepts relate to the real world, while simultaneously providing an introduction to the syntax of the Dart programming language. Learning the basics of OOP is necessary before starting any Flutter application development.

Object

Objects are the key to understanding object-oriented technology. Look around you right now and you will find many examples of real-world objects: your car, your desk, your computer, your house etc.

Real-world objects share two characteristics: state and behavior. Computers have a state (type, color, speed, capacity) and behavior (processing, playing media, browsing). A car also has a state (current gear, current pedal cadence, and current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

Class

A class is a blueprint or prototype (template) from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can clearly model state and behavior. Classes are characterized by properties and functions.
For example, if the class is a car, it may have the following properties:

• Price
• Type
• Maximum speed
• Number of seats

And it may have the following functions:
• NumberOfSeats
• NumberOfWheels
• getYearBuilt

object and class oop

Creating a Class

Use the class keyword to declare a class in Dart. A class definition starts with the keyword class followed by the class name; and the class body enclosed by a pair of
curly braces. A class code includes the class name and a group of properties or attributes which will construct the class.

Example:
The following example shows how you can create a class called car step by step:

Step 1: Declare an empty class called “car”, as illustrated in the following code:

class car {

}
main() {
}


Step 2: The car class has been created. Now, you may add some properties to this car class as illustrated in the following code:

class car {

  String type; 
  String color;
  int MaxSpeed;
  int NumOfSeats;
}

main() {

}


Step 3: To create an instance of the class or object, use the new keyword followed by the class name. Here, you can create an object depending on the “car” class (blue print) which you have created in step 1, using the following code. toyota is the name of the object which has all car class properties or attributes values.

class car {

  String type;
  String color;
  int MaxSpeed;
  int NumOfSeats;
}

main() {
  var toyota=new car();
}


As you see in the following image, the toyota object can use all the car class properties:

The following code displays the object toyota with some properties values:

class car {
  String type;
  String color;
  int MaxSpeed;
  int NumOfSeats;
}

main() {

  var toyota=new car();
  toyota.MaxSpeed=200;
  toyota.color="red";
  toyota.NumOfSeats=5;
  toyota.type="Camry";
}


For example, if you add the following command to the main() function, you can print a specific attribute or property value (type) of toyota object :

print("Car Type is: ${toyota.type}");

The run result of this code follows:

Also, you may assign a default value for these class properties such as NumOfSeats as illustrated in the following grey highlighted part of the following code:

class car {
  String type;
  String color;
  int MaxSpeed;
  int NumOfSeats = 4;
}
main() {
  var toyota=new car();
  toyota.MaxSpeed=200;
  toyota.color="red";
  toyota.type="Camry";
  print("Car Type is: ${toyota.type}");
  print("Number of Seats is : ${toyota.NumOfSeats}");
}


The run result of this program follows:

However, if you configure a specific value for NumOfSeats to the object “toyota”, it will override any other values as illustrated in the following code:

class car {

  String type;
  String color;
  int MaxSpeed;
  int NumOfSeats = 4;
}

main() {
  var toyota=new car();
  toyota.MaxSpeed=200;
  toyota.color="red";
  toyota.type="Camry";
  toyota.NumOfSeats=7;

  print("Car Type is: ${toyota.type}");
  print("Number of Seats is : ${toyota.NumOfSeats}");
}


The run output will be as follows:

Adding Methods to Classes

You can add your methods or functions to a class, then conjunct these methods with the objects which you want to initiate from this class.
In the following example, CarSpeed() method has been added to the car class as illustrated in the grey highlighted part of the following code:

class car {
  String type;
  String color;
  int MaxSpeed;
  int NumOfSeats = 4;

  void CarSpeed(){
    print("Car Speed is : $MaxSpeed");
  }

}


As illustrated in the following figure, you can use this CarSpeed()method with the toyota object:

The full code will be as follows:

class car {

  String type;
  String color;
  int MaxSpeed;
  int NumOfSeats = 4;

  void CarSpeed(){
    print("Car Speed is : $MaxSpeed");
  }
}

main() {
  var toyota=new car();
  toyota.MaxSpeed=200;
  toyota.color="red";
  toyota.type="Camry";
  toyota.NumOfSeats=7;

  print("Number of Seats is : ${toyota.NumOfSeats}");

  toyota.CarSpeed();
}


The run output of this code follows:

Providing Constructors for Your Classes

Constructor is a block of code that initializes the newly created object. A constructor resembles an instance method in Dart but it’s not considered a method because it doesn’t have a return type. Dart defines a constructor with the same name as that of the class.

Example:
In the previous example, you initialized toyota object from the car class using the following command:

var toyota= new car();


Now, you will construct the same object using a constructor which has the same class name.
First, I will create the car class again as illustrated in the following code:

class car {
  String type;
  String color;
  int MaxSpeed;
  int NumOfSeats; 
    }
main() { 

  }


Now, add the following highlighted grey color code which represents the car constructor which has the same class name.
this : The this keyword refers to the current instance of the class.

class car {
  String type;
  String color;
  int MaxSpeed;
  int NumOfSeats;

  car(type, color, MaxSpeed, NumOfSeats){
    this.type=type;
    this.color=color;
    this.MaxSpeed=MaxSpeed;
    this.NumOfSeats=NumOfSeats;
  }

  }

main() {

}


Now, you can construct or initiate an object using the car constructor as illustrated in the grey highlighted color part of the following code:

class car {
  String type;
  String color;
  int MaxSpeed;
  int NumOfSeats;

  car(type, color, MaxSpeed, NumOfSeats){
    this.type=type;
    this.color=color;
    this.MaxSpeed=MaxSpeed;
    this.NumOfSeats=NumOfSeats;
  }
  }

main() {
 var toyota=new car("Camery","red",200,5);
 print("Car Type is :${toyota.type}");
 print("Number of Seats is :${toyota.NumOfSeats}");

 }


The run output of this code follows:

Also, in the previous example, you can create the previous car constructor code in another way using the least number of lines of code by replacing the following part of a code:

car(type, color, MaxSpeed, NumOfSeats){
    this.type=type;
    this.color=color;
    this.MaxSpeed=MaxSpeed;
    this.NumOfSeats=NumOfSeats;
  }


with the following:

car(this.type, this.color , this.MaxSpeed , this.NumOfSeats);


The full code will be as follows:

class car {
  String type;
  String color;
  int MaxSpeed;
  int NumOfSeats;
  car(this.type, this.color , this.MaxSpeed , this.NumOfSeats);
}

main() {
  var toyota=new car("Camery","red",200,5);
  print("Car Type is :${toyota.type}");
  print("Number of Seats is :${toyota.NumOfSeats}");
}


The run output will be the same as the following:

Example:
Also, you can initialize an object using a constructor name. The following code creates the car class, and then using the constructor car.initialize(), you can assign the attributes values as follows:

class car {
  String type;
  String color;
  int MaxSpeed;
  int NumOfSeats;

  car.initialize(){
    type="Mini Van";
    color="Green";
    MaxSpeed=230;
    NumOfSeats=2;
      }
  }

main() {
}


Then, as you see in the following figure, you can use the constructor name in creating the toyota object. 

The full code follows:

class car {
  String type;
  String color;
  int MaxSpeed;
  int NumOfSeats;

  car.initialize(){
    type="Mini Van";
    color="Green";
    MaxSpeed=230;
    NumOfSeats=2;
      }
  }


main() {
  var toyota=new car.initialize();
  print("Car Type is :${toyota.type}");
  print("Number of Seats is :${toyota.NumOfSeats}");
}


The run output follows:

* This topic is a part of lesson 3 of Flutter Application Development course. For more information about this course, click here.