Object Oriented Programming (OOP) in Dart

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

Object Oriented Programming allows us to create objects that contain both data and functions. Obeject Oriented Programming is easier and faster to execute compared to Procedural Programming. Dart is an Object Oriented Programming Language. Some of the other OOP languages are C++ , Java , Python etc.

Various OOP features can be implemented in Dart they are :

  1. Classes
  2. Objects
  3. Data Encapsulation
  4. Inheritance
  5. Polymorphism

1.Classes

Class is a user defined data type and it contains it's own data members(Constructors , getters and setters) and member functions. A class encapsulates data for the object.
A class in Dart can be decalred by using the keyword class followed by the class name and the body of the class should be enclosed with a pair of curly braces {}

One important thing to note is the rules of identifers must be followed while declaring a class name.

A class contain constructors , fields , functions , setters and getters.

Syntax for class declaration

class class_name 
{  
   <constructors> 
   <functions> 
   <fields>
}

Example for a Class

class example
{
 string myname = "OpenGenus Foundation" ;
 void disp()
  {
   print(name) ;
  }
}

Here
example is the class name
string is the field of class
void disp() is the function of the class

2. Objects

Object is an entity. Objects are declared to access functions and data declared in a class.

Synatx for declaring an object :

var object_name = new class_name(arguments);

Example for Object declaration

var name = new example(arguments);

Here ,
object name is name
new creates a new object
example is the class name

For accessing the class properties and methods, we use . operator.

3. Data Encapsulation

Data Encapsulation is binding data and functions that use data into one unit. It is also referred to as data hiding and information hiding.
Unlike C++ and Java , Dart does not have keywords for restricting access like private , public and protected.

Encapsulation in Dart happens at the library level and not at the class level.
Any identifer that starts with an underscore _ is private to its library.

Syntax

_identifier

Example

library loggerlibrary;                            
void _log(message) 
{
   print("Log method called in the loggerlibrary message:$message");      
} 

In the above example we have defined a library with a private function.

4. Inheritance

Inheritance means the ability to create new classes from an existing one. The new created classes are called sub classes or child classes. The class from which sub classes are derived is called the super class or a parent class.
A class is inherited from another class by using the extend keyword.

Dart supports the following types of Inheritance :

  1. Single (one child class is inherited by one parent class only)
  2. Multi level (child class can inherit from another child class)

Dart does not support Multiple Inheritance.

The super keyword is used to refer to immediate parent of a class. The keyword can be used to refer to the super class version of a method or a variable.

Example for Inheritance (Single Level)

void main() 
{ 
   var obj = new model(); 
   obj.price(); 
}  
class car 
{ 
   void price() 
   { 
      print(" price of car model in the car class"); 
   } 
}  
class model extends car{}

Output

Price of car model in the car class

Do note that a child class inherits all the methods from the parent class except the constructors.

5. Polymorphism

Polymorphism is achieved through inheritance and it represents the ability of an object to copy the behavior of another object.
It means that one object can have multiple forms.
subclasses or child classes usually override instance methods, getters and setters. We can use @override to indicate that we are overriding a member.

Dart doesn’t allow overloading. To overcome this we can use argument definitions like optional and positional.

class Car extends Vehicle
{
  Car()
  {
    this.topspeed = 240;
    this.name = "Car";
  }

  void EngineStart()
  {
    print('Engine Started');
  }
}

main()
{
  car model ;
  model = new Lights(73);
  model.turnOn();
  model.goForward();
  model.turnOff();

  model = new Car();
  model.turnOn();
  model.goForward();
  model.turnOff();
}

here the object - "model" has multiple forms

With this article at OpenGenus, you must have a general idea of OOP concepts in Dart. Enjoy.

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.