When we talking about, Object Oriented Programming, first name comes into our mind is -> “encapsulation”.

Encapsulation: In object-oriented programming (OOP), encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object’s components. Encapsulation is used to hide the values or state of a structured data object inside a class, preventing direct access to them by clients in a way that could expose hidden implementation details or violate state invariance maintained by the methods.

In simple words, encapsulation means encapsulating objects and classes together. In Dart, a class encapsulates the fields, which hold the state of an object, and the methods, which define the actions of the object. Encapsulation enables you to write reusable programs. It also enables you to restrict access only to those features of an object that are declared public. All other fields and methods are private and can be used for internal object processing.

Let’s understand the concepts of classes and objects.

Dart is an object-oriented language with classes and mixin-based inheritance. Every object is an instance of a class, and all classes except Null descend from Object. Mixin-based inheritance means that although every class (except for the top class, Object?) has exactly one superclass, a class body can be reused in multiple class hierarchies. Extension methods are a way to add functionality to a class without changing the class or creating a subclass.

Using class members

Objects have members consisting of functions and data (methods and instance variables, respectively). When you call a method, you invoke it on an object: the method has access to that object’s functions and data. Use a dot (.) to refer to an instance variable or method.

Let’s understand through below code…

Code 1:

class demo{
  // member function
  void function(){
    print(‘simple member function’);
void main(){
  var ob=demo();

** Remember: like other programming languages like java, generates a ‘.class’ file according to it’s ‘.java’ file. That, .class file will be distributed further in different operating systems(windows,linux,mac). But like python it feature does not, also Dart language follows same rule as python.

Objects – A typical dart program creates many objects, which as you know, interact by invoking methods. Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network. Once an object has completed the work for which it was created, its resources are recycled for use by other objects.

Let’s understand relation between classes and objects.

Ex – ‘tiger’,’lion’,’cheetah’ all are animals. So here, class = animal , objects = tiger, lion, cheetah

Same as, ‘parrot’,’crow’,’owl’ all birds. So here, class = bird , objects = parrot, crow, owl

Let’s see below code…

Code 2:

class animal{
  void allanimals(){
    print(“animals are non-vegeterians”);
void main(){
  /* class ‘animal’ objects -> ‘tiger’,’lion’,’cheetah’
  constructor -> animal()
  var tiger=animal();
  var lion=animal();
  var cheetah=animal();

** remember: before mentioning any class name use keyword ‘var’ before.

Note: An object is an instance of class, and the process of creating an object is called ‘instantiation’.

Let’s understand another implementation of classes and objects…

Code 3:

class OS{
  // declaring member variables ‘os1′,’os2’
  String os1=”windows”;
   String os2=”UNIX”;
   // member methods(function) -> allos()
  void allos(){
    print(‘os list are -> ‘);
    print(os1+” “+os2);
void main(){
  // objects are -> ‘win’,’lin’
  var win = OS();
  var lin = OS();
  //we can use any method belongs to that class using class’s objects

Constructor – A constructor is used in the creation of an object that is an instance of a class. Typically it performs operations required to initialize the class before methods are invoked or fields are accessed. Constructors are never inherited.

In class-based, object-oriented programming, a constructor is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

Let’s work with two type of constructor here… 

  • No-argument Constructor
  • Parameterized Constructor

Let’s work with No-argument Constructor

In this constructor any type of arguments does not passed. Let’s see below code…

Code 4:

class animal{
  void cat(){
   print(‘cat sound meow!’);
  void dog(){
    print(‘dog sound bark!’);
void main(){
// constructor -> animal()
  var kitty=animal();
  var puppy=animal();

Let’s work with Parameterized Constructor

Here we pass arguments. Let’s understand through below code…

Code 5:

class animal{
  // member variable -> ‘sound’
  late String? sound;
  // member methods -> ‘cat’,’dog’
  // parameter of methods cat,dog -> variable ‘s’
  void cat(var s){
   print(‘cat sound $s’);
  void dog(var s){
    print(‘dog sound $s’);
void main(){
  // method ‘kitty’ argument -> ‘meow’, class-animal
  var kitty=animal();
  // method ‘puppy’ argument -> ‘bhow’, class-animal
  var puppy=animal();

Default Constructor

A constructor is called “Default Constructor” when it doesn’t have any parameter.

Syntax of default constructor:


Code 6:

class gender{
  // default constructor
    print(“gender can be male/female/trans-gender”);
void main(){
  /* declaring object ‘gen’ of class ‘gender’ using
  constrcutor -> gender()
  var gen=gender();

Member variables & member methods

In simple words, member methods means those functions belongs to particular class. Member variables mean variables inside class belongs to that class.

Simple syntax of member methods and variables…

class school{
var name,loc;
void func(){

From the upper code, you see member variables are -> ‘name’,’loc’ and member method (function) -> func().

Let’s see another example…

Code 7:

class dog{
  /* member variables
  name,color,height */
  late var name;
  late var color,height;
  /* member methods
  dogname() & dogType() */
  void dogname(var s){
    print(“dog name $name”);
  void dogType(var a,var b){
    print(‘dog colors $color’);
    print(‘dog height $height’);
void main(){
  var puppy = dog();
  puppy.dogType(“brown”, “hydra”);