About the Author
This blog was written by Amarjit Malick. He is currently working at Cloudcraftz as an Associate Software Developer. He has over 2+ years of experience on working on Flutter and App development. We’re grateful to have him share his insights and knowledge about Flutter with our community. If you want to know more about Amarjit, check out his LinkedIn profile.
Introduction
Object Oriented Programming is one of the most popular and widely used programming paradigms. In this article, we are going to understand how Object Oriented Programming concepts are being implemented in Dart. After reading this article, you can start your journey as a Flutter Developer and will have a good understanding of the basics of 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 behaviour.
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, behaviour, 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 defined inside a class that describes the behaviour 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, such as 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 means forms. Polymorphism is achieved through inheritance and it represents an object's ability to copy another object's behaviour. 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();
}
Conclusion
Thank You for reading this article. Hope you have gained some basic idea of how OOPs concepts are used in Dart. After reading this article, try to implement the example code by yourself. To learn more about Dart check the official documentation here. If you like this blog and want to learn more about software engineering and related topics then follow GDG Siliguri on hashnode.