Classes and Objects in Dart

·

6 min read

Cover Image for Classes and Objects in Dart
  • No matter which framework you want to learn or use

  • No matter which language you want to learn and use

  • No matter which kind of software you want to create

All of them today is based on OOPs concept and Classes and objects are base of OOP concepts. Here I will try to explain to you what is class and objects in Object Oriented Programming via a small example.

The first step to creating the microphone or any real-world thing is to create the blueprint of it.

Classes and Objects in Dart

In the blueprint, we define its properties and its functionality like

  • The color is its property

  • The mic type is its property

  • The**model number** is its property etc.

  • We can set the volume of the mic which is its functionality

  • We can mute the mic which is its functionality

  • We can turn it on or off which is its functionality etc.

  • After creating one blueprint, Then by using that blueprint, we can create a number of microphones. All microphones will have the same properties and functionality.

Dart Class Anatomy

From a programming point of view

  • The same concept here goes with Classes and Object’s terminology.

  • The blueprint is called Class and the microphones or any other device is called object here.

  • Class is a blueprint from which we can create a number of objects. All objects will have access to all properties and methods. You can also modify access according to your need.

Object Oriented Programming class is made up of four main things

  1. Properties (Instance Variables)

  2. Methods (Functions)

  3. Getters and Setters

  4. Constructors

  • These components are also called data members of the class.

  • Remember here Properties can be also called Instance Variable and Methods are also called Functions.


Syntax of Declaring Class in Dart

class class_name {
   // Properties (Instance Variables)
   // Constructor
   // Methods (Functions)
   // Getters and Setters
}
  • The class keyword is used to declare a class.

Sample Code

class Mobile {
  String color;
  String brandName;

  String calling() {
    return "Mobile can do calling";
  }

  String musicPlay() {
    return "Mobile can play Music";
  }
}

Syntax of Creating Object in Dart

var object_name = new class_name (arguments);

Sample Code

var myMobile = new Mobile();

Accessing Properties and Methods Of Class in Dart

  • As I mentioned earlier all the properties and methods of a class can be accessed via the class’s object.

  • For accessing the class’s properties and methods, we use ‘.’ (dot notation) also called a period.

Syntax of accessing properties and methods

// Accessing Properties
   object_name.property_name;
// Accessing Methods
   object_name.method_name;

Sample Code

// Accessing properties
    myMobile.color;
    myMobile.brandName;

// Accessing methods
    myMobile.calling();
    myMobile.musicPlay();

Let’s understand all concepts by one complete Dart Program

class Mobile {
  String color; // Property
  String brandName;
  String modelName;

  String calling() { // Method Creation
    return "Mobile can do calling";
  }

  String musicPlay() {
    return "Mobile can play Music";
  }

  String videoPlay() {
    return "Mobile can play video";
  }
}

main() {
  var myMobile = new Mobile(); // Creating Object

  myMobile.color = "White"; // Accessing Class's Property
  myMobile.brandName = "Apple Inc.";
  myMobile.modelName = "iPhone 14";

  print(myMobile.color);
  print(myMobile.modelName);
  print(myMobile.brandName);
  print(myMobile.calling());
  print(myMobile.musicPlay());
  print(myMobile.videoPlay());
}

Output
White
iPhone 14
Apple Inc.
Mobile can do calling
Mobile can play Music
Mobile can play video
  • Remember guys, classes, and objects are basic building blocks of Object-Oriented Programming.

  • All big frameworks like Flutter, React, Django, Angular, and big software like IDEs, Web Apps, Mobile Apps, Server side apps, and also the programming language itself are based on OOPs concepts. So please have a clear clarity and strong understanding of these concepts.

Conclusion

The article explains the fundamental concepts of Classes and Objects in Object-Oriented Programming (OOP), using an analogy of creating blueprints for real-world objects. In OOP, a class serves as a blueprint that defines properties (instance variables) and methods (functions) for creating objects. The key points covered are as follows:

  1. Blueprint and Real-world Analogies: The process of creating real-world objects starts with designing a blueprint that defines properties (e.g., color, type, model number) and functionality (e.g., adjusting volume, muting, powering on/off). Similarly, in programming, a class represents this blueprint for creating objects.

  2. Class and Object Definitions:

    • Class: A class is a blueprint that encompasses properties and methods. It defines the structure and behavior of objects that can be created based on it.

    • Object: An object is an instance created from a class. It inherits the properties and methods defined in the class.

  3. Components of a Class:

    • Properties (Instance Variables): These define the characteristics or attributes of an object.

    • Methods (Functions): These define the actions or behaviors that an object can perform.

    • Getters and Setters: These are special methods that allow controlled access to and modification of object properties.

    • Constructors: Constructors are special methods used to initialize object properties when an object is created.

  4. Accessing Properties and Methods:

    • The properties and methods of a class are accessed through objects using the dot notation (e.g., object.property, object.method()).

    • Objects created from the same class share the same properties and methods defined in the class.

  5. Significance of OOP Concepts:

    • OOP concepts like classes and objects are foundational to modern software development.

    • Major frameworks (Flutter, React, Django, Angular) and various types of software (IDEs, web apps, mobile apps, server-side apps) are built upon these concepts.

The overall message of the article is to emphasize the importance of understanding classes and objects as essential building blocks in programming, given their pervasive role in various software development paradigms and frameworks.

That’s it for classes and objects guys. Also, feel free to let me know if I miss something, I’d love to learn it from you. Till then Keep Loving, Keep Coding. And I’ll surely catch you up in the next article.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitant that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

Learn More about Dart and Flutter

Follow me for more such content