What is Object Oriented Programming?

What is Object Oriented Programming?

In the last post I wrote about "Why we use classes and objects?". In this post, let's discuss what is object oriented programming(specifically regarding classes and objects) in detail.

Let's start with the definition. Object oriented programming paradigm is a programming paradigm based on objects. Some of the languages following Object oriented paradigm are Java, C++, Python, Scala, Ruby. I will talk specifically about Java and C++.

Objects primarily contain data and methods. Also, objects are instances of a class. So let's start from there.

The Wiki definition of class is:

In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).

To put it in simple terms it is a blueprint for creating instances, and contains two entities: member variables and member functions(or methods).

Member variables are variables which are present in a class. These variables can further be classified into two types: class variables and instance variables. Class variables (or static member variables) are those variables which are created once and shared among all the instances of a class. These are used in case the data needs to be shared among instances. Instance variables are those variables where each instance has its own independent copy.

Member functions or methods: A function defined within a class is called a method. In case of C++, these are commonly known as member functions. These are used to provide functionality to objects. Let's see a simple class in C++:

class Car
{
    public:                      //access specifier
    string type = "Car";         //data members
    string id;
    int model;
    Car(string i, int m)        //Constructor
    {
        id = i;
        model = m;
    }
    void details()               //methods or member functions 
    {
        cout<<"Car id: "<<id<<"\n";
        cout<<"Car model: "<<model<<"\n";
    }
    void move()
    {
        cout<<"Moving"<<"\n";
    }
    void stop(){
        cout<<"Stop moving"<<"\n";
    }
};

The same thing in Java can be as follows:

class Car
{
    String type = "Car";
    String id;
    int model;
    Car(String i, int m)
    {
        id = i;
        model = m;
    }
    void details()
    {
        System.out.println("Car id: " + id + "\n");
        System.out.println("Car model: " + model + "\n");

    }
    void move()
    {
        System.out.println("Moving");
    }
    void stop(){
        System.out.println("Stop moving");
    }
}

Here you came across two new terms - access specifier and constructor. Let me explain each of them.

Access specifiers : Access specifiers(also known as access modifiers) are used to determine the visibility of class members(data and methods). The three access specifiers available in C++ are public, private and protected. In Java, there is an additional access modifier - default.

Constructor: A Constructor is a special function which initialises the objects of a class. It is called every time an object is created implicitly.

Some points to note regarding constructors:

  1. They have the same name as the class name.
  2. They do not have a return type.
  3. They are called every time an object is created.
  4. In case you do not provide a constructor, the compiler itself provides default constructor.

That's enough of classes, now let's look at what objects are.

Object : An object is an instance of a class. When an object is initialised, the space is allocated in memory. As I mentioned earlier a class is just a blue print, and hence is not allocated memory. It is the objects which occupy the memory.

Let's see how to create an object for the class Car in C++:

Car c("Class A", 101);

The same thing in Java can be done as follows:

Car c = new Car("Class A",  101);

Now the 2 parameters passed: "Class A" and 101 are used to initialise the id and model data.

Objects represent the real world entities whereas class is just the blueprint.

So in this post, I discussed regarding classes and objects, how are they created(in Java and C++), explained regarding each component of a class.

Keep Learning :)