A Basic Introduction to Object Oriented Programming in Dart

A Basic Introduction to Object Oriented Programming in Dart

What is Object Oriented Programming?

From the name, we can easily understand that it is a programming pattern that rounds around an object or entity. An object can be defined as a data field that has unique attributes and behavior.

Building Blocks of Object Oriented Programming

Class

A user-defined data type that acts as the blueprint for individual objects, attributes, and methods.

classes are logical entities, not data structure

//example of a class
class Person {
  String name;
  int age;

  void sayHello() {
    print('Hello, my name is $name and I am $age years old.');
  }
}

Object

An instance of a class created with specifically defined data. It is a real-world entity that has attributes, behavior, and properties. Object contains member functions, variables that we have defined in the class.

Person person = new Person();
person.name = 'John';
person.age = 30;
person.sayHello();

Methods

A Method is a function that is defined inside a class that describes the behavior of an object.

class Person {
  String name;
  int age;

//example of method
  void sayHello() {
    print('Hello, my name is $name and I am $age years old.');
  }
}

Attributes

Objects will have data stored in the attributes field. Class attributes belong to the class itself.

class Person {
//attributes
  String name;
  int age;

  void sayHello() {
    print('Hello, my name is $name and I am $age years old.');
  }
}

Main Principles of Object Oriented Programming

OOP rests on the following four pillars:

Encapsulation

Encapsulation means the action of enclosing something. In OOP Encapsulation is the concept of hiding the implementation details of a class from its users.

Dart does not have keywords for restricting access like private, public, and protected. Any identifier that starts with an underscore _ is private to its library.

class BankAccount {
  String _accountNumber; // private variable
  double _balance; // private variable

  BankAccount(this._accountNumber, this._balance); // constructor
}

Abstraction

In OOPs Abstraction is like taking away / abstracting out some common functionalities. Objects only reveal internal mechanisms that are relevant for the use of other objects, hiding any unnecessary implementation code. The derived class can have its functionality extended.

abstract class Shape {
  double getArea();
  double getPerimeter();
}

class Rectangle implements Shape {
  final double _length;
  final double _width;

  Rectangle(this._length, this._width);

  @override
  double getArea() {
    return _length * _width;
  }

  @override
  double getPerimeter() {
    return 2 * (_length + _width);
  }
}

void main() {
    Shape shape = Rectangle(10.0, 5.0);
    print('Area: ${shape.getArea()}');
    print('Perimeter: ${shape.getPerimeter()}');
}

Inheritance

Inheritance means the ability to create new classes from an existing one, more simply, to inherit something from the other class.

In Dart, we can create a subclass by extending a base class using the extends keyword.

Dart does not support multiple inheritance.

class Person {
  String? name;
  int? age;

  void sayHello() {
    print('Hello, my name is $name and I am $age years old.');
  }
}

class Student extends Person {
  String? school;

  void study() {
    print('$name is studying at $school.');
  }
}

void main() {
  Student student = Student();
  student.name = 'Mary';
  student.age = 20;
  student.school = 'ABC University';
  student.sayHello();
  student.study();
}

Polymorphism

Poly means many and morphs mean forms. Polymorphism is achieved through inheritance and it represents the ability of an object to copy the behavior of another object. Subclasses or child classes usually override instance methods, getters, and setters.

We can use @override to indicate that we are overriding a member.

Dart does not support method overloading.

class Animal {
  void makeSound() {
    print('Animal makes a sound.');
  }
}

class Dog extends Animal {
  @override
  void makeSound() {
      print('Dog barks.');
  }
}

void main() {
  Animal animal = Animal();
  Dog dog = Dog();

  animal.makeSound();
  dog.makeSound();
}

Thank You for reading this article. Hope you have gained some basic idea of OOPs in Dart. If you have any feedback/queries, leave them in the comments.