Object Oriented Programming (OOP)
Object-oriented programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints, which are used to create individual instances of objects. There are many object-oriented programming languages, including JavaScript, C++, Java, and Python.
First, we need to clarify what the class and object are. A class is a blueprint or template from which objects are created. But objects are instances of a class. For example, let’s have the vehicle class.
Class: Vehicle
Objects: Car, Van, Lorry,...
We can identify four basic concepts in object-oriented programming.
- Encapsulation
- Inheritance
- Abstraction
- Polymorphism
Encapsulation: Encapsulation means containing all important information inside an object and only exposing selected information to the outside world. Simply put, this is called information hiding. We used the private, public, and protected keywords for defining the hiding level of the information. We use attributes and behaviours as information.
According to the vehicle example, a vehicle class can contain the following attributes and behaviors.
Attributes: Price, Brand, Colour
Behaviours: Drive(), breaks(), accelerate()
Let’s assume that the price of a vehicle needs to be hidden from the outside world. So we can use private keywords for the price attributes, and others can use the public keyword. Now the price cannot be accessed from the outside world.
Inheritance: Inheritance is used to inherit the attributes and behaviours from one class to another. It means if a child class extends the parent class, the child class can have the attributes and behaviours of the parent class. So we call inheritance to support reusability.
Abstraction: Abstraction means that the user interacts with only selected attributes and methods of an object. Abstraction uses simplified, high-level tools to access a complex object. Usually abstraction can be defined as hiding the lower level from a class.
According to the previous vehicle example, let’s assume the vehicle class is an abstract class. The methods in the vehicle class can be used in their child classes, but they hide the implementations from the child class.
Polymorphism: Polymorphism means designing objects to share behaviours. Mainly, we use two methods in polymorphism.
- Method overriding
- Method overloading
Method overriding: In method overriding, a child class can provide a different implementation than its parent class. Method overriding uses runtime polymorphism.
According to the previous example, we can override the drive() method in the vehicle class by using different implementations.
Method overloading: In method overloading, methods or functions may have the same name but a different number of parameters passed into the method call. Different results may occur depending on the number of parameters passed in. Method overloading uses the compile time polymorphism.
Benefits of OOP
- The object-oriented programming model makes complex scenarios as simple scenarios
- Reusable components across the program
- Allows to have class-specific behaviours using polymorphism
- Secure the needed information from encapsulation