What is OOP??

Prabhashi Wijesinghe
LinkIT
Published in
6 min readJun 4, 2022

--

Hello Everyone!!!

Artwork by Author

Do you know what OOP is??🤨

OOP stands for Object Oriented Programming. OOP is a programming paradigm built on the concept of objects and classes and it aims to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function. Most of the modern programming languages except the C language follow OOPs. The popular object-oriented languages are Java, Python, C#, C++, PHP, etc.

Building blocks of OOP

What are the building blocks of OOP??

  • Classes
  • Objects
  • Methods
  • Attributes

are the fundamental building blocks of OOP.

Let’s explain these four building blocks using a real-world example.😇😇

Let’s take a car as an example.

Artwork by Author

A class is a blueprint or prototype that is defined by the user and used to build objects. It denotes the collection of attributes or methods that are common to all objects of the same type. Class doesn’t take up any space. In our example car is the class. car is a broad category.

Using the blueprint of the car we can create different types of cars. These instances of the car are called objects. Therefore an instance of a class can be defined as an Object. An object holds an address and occupies memory space. Simply, objects represent real-world entities.

These objects have states and behaviors. As you can see, the above car has a color, a model, and a brand. These are the attributes of the class car. Attributes reflect an object’s characteristics. A class specifies which properties, such as color, an instance of this type will have, but not the value of those attributes. Individual objects have data stored in the attributes field when they are instantiated.

Finally methods.. Methods define the behaviors. Methods carry out operations, such as returning information about an object or updating its data. The code for the method is specified in the class definition. Individual objects can invoke the methods defined in the class when they are instantiated. This car class has methods like repairing the engine and repainting.

Okay.. I think now you have some basic understanding of the building blocks of OOP and let’s move into Pillars of OOP. 🤗

Four Pillars of OOP

  • Inheritance
  • Encapsulation
  • Abstraction
  • Polymorphism

are the four pillars of OOP. Let’s go through one by one of these topics.

  1. Inheritance

Classes can inherit features from other classes thanks to inheritance. Child classes inherit attributes and behaviors from their parent classes. Reusability is aided via inheritance, which means that if we want to construct a new class but there is already a class with some of the code we need, we can derive our new class from the old one. As well as the new class can have its own attributes and methods.

Artwork by Author

As shown in the above figure a person can walk, talk and eat. Person is the parent class and it has walk(), talk(), and eat() methods. Science teacher and cricketer are also a person and they also can walk, talk and eat. Therefore the child classes, science teacher, and cricketer inherit the walk(), talk(), and eat() methods from the parent class. As well as they can have their own methods like teachScience() and playCricket().

2. Encapsulation

Encapsulation is the process of wrapping code and data into a single entity. Encapsulation encloses all critical information within an object and only reveals a subset of it to the outside world. Code within the class template defines attributes and behaviors. This enclosed information can only be accessed through the member functions of the class in which they are specified. To accomplish encapsulation we have to declare all variables in the class as private and write public methods in the class to set and get the values of hidden variables. Encapsulation improves security.

Let’s pretend you are an undergraduate and you have a student account on your university website. Your GPA mark will be known as public if your GPA mark variable is declared as a public variable on the university website. In this situation, anyone can see your GPA mark🥴. Therefore they can declare the GPA mark variable as private in order to prevent others from seeing your GPA mark. The individual who needs to examine the GPA mark will be able to do so only through methods specified within that class, and this method will ask for your index number, as well as your password for authentication😉.

3. Abstraction

Abstraction is the process of identifying only the important characteristics of an object while disregarding the irrelevant elements. The states and behaviors of an object assist to distinguish it from other things of the same type, as well as classify and group them.

Imagine that you are going to a shop by bus. The driver sees a child crossing the road and suddenly applies the brakes to stop the bus😧. The driver knows applying the brakes will stop the bus, but he has no idea about the inner mechanism of the bus or the implementation of the accelerator and brakes🙄. There is no point in knowing the inner mechanism of applying a brake and only knowing that applying a brake will stop the bus is enough for the driver. So he ignores the mechanisms. This is the concept of abstraction.

Abstraction hides complex codes and only shows the simple and high-level user interfaces. It improves security as well.

4. Polymorphism

Polymorphism refers to the fact that something exists in multiple forms. In simple terms, polymorphism is the ability of a message to be displayed in various formats.

Artwork by Author

A real-world example of polymorphism is a person having distinct characteristics at the same time. At the same time, a woman can work as a mother, a wife, and an employee. As a result, the same person behaves differently in different situations. This is polymorphism.

Method overriding and method overloading are two ways that polymorphism allows the same method to perform various actions.

Method overriding is used in runtime polymorphism. When a child class overrides a parent class’s method, the child class can provide a different implementation.

Method overloading is used in compile time polymorphism. Although two methods or functions may have the same name, the number of parameters given in the method call may differ. Depending on the number of parameters entered, different results may be obtained.

Okay.. Those are the four pillars of OOP. Now let’s see the advantages of OOP in brief.

Advantages of OOP

  • Code reusability- We can minimize duplicate code and extend the use of existing classes by using inheritance.
  • Code flexibility- Polymorphism is the notion that allows for flexibility in code. Polymorphism offers simplicity and extensibility as well.
  • Effective problem solving- Breaking down problems into smaller pieces can help you solve the problems effectively.
  • Easy troubleshooting- When OOP is used, the user knows exactly where to look in the code when an error occurs.
  • Security- Abstraction, and encapsulation improve the security of the code.

Now we have reached the end of this article. Hope you gained some new knowledge..😊

Thanks for reading..!!!

--

--

Prabhashi Wijesinghe
LinkIT

A dedicated and hardworking IT undergraduate, willing to share and gain information related to IT industry👩‍💻