In this article, I will explain about the concept of OOP (object-oriented Programing) with simple examples.

WHAT IS OBJECT-ORIENTED PROGRAMMING (OOPS)?

Object-oriented programming (OOP), is a method of organizing software design around data or objects rather than logic and functions. OOP focuses on the objects that developers want to manipulate, rather than the logic that must be used to do so. OOP is a method that facilitates collaborative development. Projects are broken down into groups because they are organized using an object-oriented programming model. OOP also offers code reuse, scalability, and efficiency.

A lot of programming languages out there use OOPS in one way or another, starting from C++, Java, C sharp, dot net, and Salesforce Apex language.

And in this article, we’ll be discussing about exactly this, OOPS in Programing, and Apex as well.

So, let’s try to break it down one by one. We say, OOPS is an object-oriented programming Language, so, what is an object? an object is a real-world entity like an Employee, Cat, or Car. Let’s see a few examples about objects Like we said Employee is an object so for storing employee information we store Employee Name, Employee Number, Age, Department, and so on. An Employee is a real-world entity so we can say, an Employee is an object. Same in this case Cat is an object and a car is also an object, and similarly, a mobile, a laptop, a person, a house, a country, all of these things are objects. Anything that you see in the real world, is an object. Even a Website is also an object; all of these objects have some properties, and let’s take our employee object as an example.

An Employee has a name, and age, they should also have an employee number assigned to themselves and a department name as well. So, all of these different attributes of an Employee are called properties.

Along with these properties, an object also has behaviors like, an Employee has to mark his attendance or get salary / Allowances or submit annual appraisal so, all of these are behaviors of an Employee, So, these properties and these behaviors define an object, or you can also say, they give an object a definition without these properties or behaviors, an object is nothing, it’s null.

Now, let’s move to the next question.

How do you represent an object in a programming language?

So, most of the programming languages, they use a class to represent an object or to give an object a definition. A class is used to store the properties and behaviors of an object, where the properties are called variables and behaviors are called methods or functions.

So in simple English, I would like to define a real-world identity in a computer program, we use classes, or you can also say, a class is a bundle of an object’s properties and behaviors, and generally you name your class after the object

it is defining. But it is totally up to you, how you want to name your classes.

Now, let’s take a look, at what a class could look like for an Employee object. First, you write this special keyword called Class to tell your Apex compiler that you are about to create a class and then you write the class name. So, here we have given the class name as an Employee. Then you declare all the properties of that object as variables.

and you are already aware of variables and in the end, you write functions or methods to define your object’s behavior, like marking attendance or getting salary or executing appraisal, and so on.

Now let’s move on to our point the pillars of OOPS. OOPS has many concepts, but it has four main pillars.

1). Encapsulation

Encapsulation is a method is about bundling your code. let’s brief encapsulation with an example. I will pick an example from this article that I have earlier discussed about Employee number this is how we used to store the employee numbers. We did not have any mechanism to store all the properties of an Employee,

right? We could not store the employee name, numbers, ages,s, and department names together anywhere, because we did not have any mechanism to store all these properties of Employees. But now, let’s take a look at this Employee class and we can clearly see, how well it defines an Employee. It contains all the properties of Employee and you can add more properties as well. It also defines the Employee behaviors like marking the attendance, processing the salary, and submitting the appraisal; this bundling of variables and methods is called encapsulation, and in OOPS, we do it with the help of classes.

2). Abstraction

Abstraction is a method is about hiding your code. let’s brief this second pillar of OOP with an example. We already know about employee objects and It is possible that some of your properties of an employee should remain secret and no other piece of code or class should be able to see these properties directly. Let’s try to explain it with a real-world example. Let’s say, you have a gaming controller which has a few buttons to play your games, but you should not be worried about how these buttons give commands to your game or how the internal structure of a gaming controller looks like, or how different wires are joined together in your gaming controller’s socket all of these functions are hidden from us and this is called abstraction andnd OOPS also provides us this Abstraction feature, where you can hide some complex piece of code or some sensitive piece of code or properties from all the other classes or objects.

Let’s move on to Employee class again. And for now, we are not using Abstraction here, although the Abstraction comes by default with each and

every variable method or class that you declare, which is like the default scope of the class and the variables.  However, you could rewrite your class like this.

You can make your properties private, so no other class can see these properties.

However, you can make some of your methods public. So that, the other classes can interact with this class, with the help of these public methods. These keywords like private or public are called access modifiers, which control the level of abstraction

for a method, or for a class, or a variable. Apex has three access modifiers, private,

public, and global. we will be seeing about all these access modifiers in detail in next article.

3). Inheritance.

Abstraction is a method that is about designing your code layout. Now, let’s explain with the picture below. We all know the meaning of inheritance, as being a human,

we inherit some of our qualities or habits from our father along with our DNA, and we also inherit some of our qualities from our mother as well. And on top of that, we have our own unique qualities and habits. The same thing is possible with OOPS as well. Let’s explain this with the help of an example. You have three shapes, the first one is a rectangle, the second is a triangle, and the third one is a circle, and all these different shapes have a few things in common. They all have a surface area and a perimeter. Although the logic of area calculation and perimeter calculation cannot be same for all these different shapes, but each shape can define their own formula for area calculation and the perimeter calculation. So, we all agree that a rectangle or a triangle or a circle is nothing but a shape, and they all inherit some of the shape properties like, calculating the area or calculating the perimeter. Now, let’s brief on how you implement inheritance in Apex.

So first, we define the interface with the help of a keyword called interface, and then you give it a name. So, here we are naming our interface as shape. This interface should have a base layout for a shape, like its behaviors. And whenever the shape is being implemented or inherited somewhere, then these common behaviors need to be defined. So, our common behaviors across all shapes are, they all must have a method to calculate the area and another method to calculate the perimeter.

The interface can not give a definition to these two functions or methods, because each shape that will inherit this interface, will define its own formula to calculate the area and perimeter. Here, you can only declare your methods, but can not give them definition. Now we have a class called rectangle and this class inherits our shape interface with a special keyword called implements, any class can inherit multiple interfaces.

So, let’s say, along with the shape, if you want to inherit another interface as well then you can give the name of that interface here after a comma. Now, as soon as a class inherits an interface, it must give the definition to all the methods in that interface. So In our interface, we have two methods, the first is getArea(), and the second is getPerimeter(), So our Apex class rectangle must give the definition to both of these functions. Since a rectangle has its own formula to calculate the area and perimeter, we are going to write our logic in these two methods.

Very similar to this, you can have other classes like Triangle and Circle as well, which have their own logic to calculate the area and the perimeter. So remember, if a class inherits an interface, you must give the definition to all the methods which are declared in an Interface. If you don’t give the definition to these methods, you will not be able to save your code, and you will get a compile time error. So, the interface is a nice way to create a layout for common functions that all the classes that are inheriting that interface must define. So, you are kind of forcing some piece of code on the developers who are inheriting your interface and we will be using a few interfaces in this course that are available in Apex and we will also create our own interface as well.

4). Polymorphism.

Polymorphism is a method that is about giving multiple definitions or behaviors of an object like a teacher is an object and he has different behavior in class and different behavior at home. Now, let’s brief with few examples In simple English, the polymorphism is, one with many forms, and a good example to understand the polymorphism is the power button on your mobiles or on your laptops, which does multiple functions like, switch on your device, switching off your device, switch on the display or switching off the display. So, one button does many things.

Another example could be the send button in a chat application, where you get different options to send different things. So like, with one send button, you can also send text, you can also send your audio, video and images as well.

And now let’s explain how polymorphism looks in Apex.

It’s all about giving multiple definition to a method with same name. So, here we have three different methods and all of them are named sent. So, the function name is the same, but the parameters are different. The first one accepts a string parameter, and it is about sending a text message.

The second one accepts a blob object or a binary object, which could be an image or video binary, and this one is about sending a single video or a message.

The third one accepts a list of blob objects on binary objects to send multiple videos or images. So, although the name of the function is the same, we are getting three different definitions of the same function. The thing is, we are using different parameters here. You cannot create a function with the same name and same parameters again, so the function signature must be different. You can have the same name, but at least the parameters should be different. Else, again you will get a compile time exception or error. So, I hope I gave you a good idea about the OOPS and its pillars, but if you still find it challenging to understand the OOPS concepts, then I highly recommend you to contact me at [email protected] or write any comment.