Object Oriented Programming Aka OOPs

Object Oriented Programming article is an extension of my previous article, the types of programming languages. Initially, the Object Oriented Programming concept was not a thing. The computers were very new and computer scientists were searching for solutions to various types of problems through programming, those were the days of just writing more and more complicated code. If the problem is more complex, the code becomes even more complex. We did not know but we were moving towards procedure-oriented programming.

Object Oriented-Programming-Image

A point came when the programmers were tired of writing thousands of lines of code to solve a simple problem. They also realized they were duplicating the code a lot. Every time they wanted to print something on the screen, they were writing the code to print the statement on the screen. They felt they need a print function so that they don’t write the same code time and again. This idea of writing predefined functions was the beginning of the procedure-oriented programming language.

With procedure-oriented programming language, we started dividing problems into pieces. At the next level, we again divided those pieces into smaller pieces. We keep on dividing them until every piece becomes a single function (method). This approach was also called a top-down approach because every time when we had a problem, we looked at it from top to bottom and started dividing it from top to bottom.

For example, If we needed to make a calculator, we divided its functionalities into two parts, Floating Point Operations and Integers Operations. We wrote the functions of each operation such as division, subtraction, adding, and multiplications.

Why Object Oriented Programming Concept is Needed?

What is an object

Anything that you can see or tough is an object.

The above line is the shortest definition of an object.

Object Oriented Programming is the Mimicry of Nature

Object Oriented programming is needed because it is very close to nature. We humans have already mastered this concept in our daily lives. Even newborn babies use this concept. Ask a mother about her baby when the baby recognizes her voice and face and stops crying. This happens because the baby is using the voice of the mother as a property to recognize her. Later on when the mind of the baby develops the baby learns there are other properties added to the mother that the baby can use to recognize her such as facial features, facial expressions, even the way the mother holds the baby in her hands.

Later on, the baby understands the mother has a function too, the mother can give the food so the baby starts crying. The baby recognizes the father also has a function that he can take the baby out for a walk so the baby pursues the dad or the mother to do that.

When this baby becomes a kid he knows who has the function of buying him or her a bicycle. This process is recognizing things as the object goes on the entire life of this kid.

What do we learn from this example of baby and mother

From this example, we see the baby is considering the mother as an object, and recognizing this object has two things associated with it. Properties and functions. The properties are also called looks and feels. The functions are the tasks that an object can perform.

Even the paperweight on your table is an object because this paperweight has some looks and feels and it is performing a function of stopping the papers from flying away. The brick in a wall is an object because it has looks and feels and performing the function of being a part of the wall.

Object Oriented Programming is needed because data security is the most important aspect of programming. Earlier this subject of data security was not considered by computer scientists. Later on, they realized they are supposed to address a very crucial problem in the programming languages.

In the beginning, the problems that we were resolving using computers were very basic. Later on, humans started resolving the problems that were very close to human lives. At this point, we were not just adding a bunch of numbers together or dividing some big numbers from one another. We started storing data on computers too. The data cannot be left loose.

Let me give you an example, you would not like it if the data of your medical history is accessible by some other person who has nothing to do with this data. If a doctor is accessing this data because you are going through the treatment under this doctor you would be fine with it because this data will help the doctor for curing the disease. Once the treatment is completed, you would want to stop the doctor from accessing the data.

This example tells you that the data does not come alone. The responsibility of keeping the data locked and secured also comes with the data. This aspect of data security was considered when designing the object oriented programming concept. This is the biggest reason why the OOP concept is the best. There are more things that were addressed with Object Oriented programming that you will learn when we discuss OOP.

What is Object Oriented Programming

Object Oriented Programming is a concept of creating real-world objects into code. It has 6 sub-concepts to it.

  1. Object
  2. Class
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  6. Encapsulation

Object

Why do we need to create real objects into code?

Suppose, you want to know how hard is it playing golf. What do you think is the best way to do it? There will be some guys who will go to some good golf players and ask them but that will not serve the purpose completely. What if you go to a club and take the membership and gain personal experience by actually playing it.

The same approach is used in object oriented programming. We try to create real objects in code and then simulate them to create actual situations. The best example of this simulation is the games nowadays. The games like America’s Army, Counter-Strike, Valorant, PUBG etcetera.

The best thing about this way of programming is that you feel like you are creating something that you have known for years and it feels interesting too. You are the one who gives properties to the objects. You are the one who gives functionality to the objects. If you have created a gun in programming you can develop some functionalities that do not exist in the real world. You can create a gun that shoots 2, 3, 4 bullets on a single trigger press. You can go beyond what nature has to offer by using your imagination. By using this approach, your imagination is the limit of your creativity.

How do We Create an Object in Programming

We create an object into programming by creating a class.

Class

A class is a blueprint of an object. Every time when you are in need to create an object that means you need to create a class.

The syntax of creating a class in C# is mentioned below.

class Car
    {
    }

The above-mentioned syntax is an example of a class named Car. The class has no properties or functions at this point. We can add the properties and functions to this class and this is how it is done. Look at the class below.

class Car
    {
        public string Color { get; set; } = "White";
        public int Speed { get; set; } = 0;
        public void IsRunning()
        {
            if (Speed > 0)
            {
                Console.WriteLine("Car is running...");
            }
            else if (Speed == 0)
            {
                Console.WriteLine("The Car is stopped.");
            }
        }
        public void ApplyBreak()
        {
            this.Speed = 0;
        }
    }

You can see the above class has two properties called Color and Speed. The car also has two functions called IsRunning and ApplyBreak. IsRunning function tells you if the car is running or stopped. UseBreak function changes the value of the Speed property and makes it 0, which means when you use ApplyBreak method of the class. It stops the car.

Both properties have default values. By Default color is Red and the speed is 0. Just like in the real world when you see a car, the car will have some color and it will either 0 or more than 0. That simply means it will either be running or stopped. This is how we simulate the real world objects into code.

The functionality of this car can further be increased by adding more methods to it.

Why do we create an Object of a Class

In Order to use this car class, we will have to create an object of this Car class. Creating an object of a class is like taking the remote controller of the class in your hands. You can use that remote controller and make use of the call as per your convenience.

Let me show you how to create an object of the class that we have created above.

using System;
namespace CreatingAClass
{
    class Car
    {
        public string color { get; set; } = "White";
        public int Speed { get; set; } = 0;
        public void IsRunning()
        {
            if (Speed > 0)
            {
                Console.WriteLine("Car is running...");
            }
            else if (Speed == 0)
            {
                Console.WriteLine("The Car is stopped.");
            }
        }
        public void ApplyBreak()
        {
            this.Speed = 0;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Car car = new Car();
            Console.WriteLine(car.Speed);
            car.IsRunning();
            car.ApplyBreak();
            car.IsRunning();
            Console.ReadLine();
        }
    }
}

The output of the Above Program

Look at the screenshot below, the highlighted word car is an object of Car Class. The Object word car is used to manipulate the properties of the class and use access to the methods of the class. In programming terminology, this word car is referred to as the name of Car object.

I hope you have understood the concept of Object-Oriented Programming Langauge and why it is so famous. In this article, I have already explained about object and class.

I have also explained what it means when we say the followings.

  1. Create a Class.
  2. Create an object of a Class.

I will keep on writing articles explaining about the other Pillars of Object Orinted Programming. The next is Inheritance.

Till Then Good Luck Guys.

Share your love
Nadeem
Nadeem

Leave a Reply

Your email address will not be published. Required fields are marked *