Object-Oriented Programming

Dart is an object-oriented programming language, which means every value in a Dart is an object. A number is also an object in Dart language. Dart programming supports the concept of OOPs features like objects, classes, interfaces, etc.

Object: An object is an entity, which has state and behavior. It can be physical or logical. In Dart, every value is an object, even primitive values like text and number. Dart can also allow you to build your custom object to express more complex relations between data.

Class: A class is a collection of objects. It means the objects are created with the help of classes because every object needs a blueprint based on which you can create an individual object. A class definition includes the following things:

  • Fields
  • Methods
  • Constructor
  • Getters and setters

Let us see an example, which helps you to understand the OOPs concept easily.

class Mobile {  
  // Property Declaration  
  String color, brandName, modelName;  
    
  // Method Creation  
  String calling() {  
    return "Mobile can do call to everyone.";  
  }  
  String musicPlay() {  
    return "Mobile can play all types of Music.";  
  }  
  String clickPicture() {  
    return "Mobile can take pictures.";  
  }  
}  
  
void main() {  
  // Object Creation  
  var myMob = new Mobile();   
    
  // Accessing Class's Property  
  myMob.color = "Black";   
  myMob.brandName = "Apple Inc.";  
  myMob.modelName = "iPhone 11 Pro";  
    
  //Display Output  
  print(myMob.color);  
  print(myMob.modelName);  
  print(myMob.brandName);  
  print(myMob.calling());  
  print(myMob.musicPlay());  
  print(myMob.clickPicture());  
}  

In the above example, we define a class Mobile, which has three variables of string type and three functions or methods. Then, we create a main function which Dart will execute first when your app starts. Inside the main, we create an object to access the class’s properties. Finally, we print the output.


Comments

Leave a Reply

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