Object Oriented Programming

Object Oriented Programming

Object Oriented programming is a high-level programming model different from procedural programming. This model focuses on data and object rather than function and logic.

To work with OOP, developers have to combine a group of related variables and their functions into a unit. This unit is the object. The practice is called data modelling. Data modelling in OOP is used to represent real-life objects with their properties and behaviours tied together. Manipulation is done on the objects themselves - their states and behaviours rather than the data and functions.

The first object-oriented programming language developed was called Simula. It was developed in the 1960's at the Norwegian Computing Center in Oslo and is generally accepted as the first language with general programming language features. Despite this, another language, Smalltalk, developed in the 1980s is considered the only true object-oriented language and it set the path which other object-oriented languages must follow.
Many modern programming languages like Python and JavaScript support object-oriented programming as well as procedural. Languages like Java are purely object oriented while languages like C++ are regarded as object-oriented languages as well as a programming language with object-oriented features. This is because it is less object-oriented than some other languages like Ruby.

Difference between Object Oriented Programming and Procedural Programming

Procedural programming is the most common type of programming used in languages like JavaScript and Python. It is the most widely taught and the first style of programming most developers encounter.
The code for procedural follows a top-down approach of logical steps and algorithms. It separates data and procedures. These procedures are called functions, routines and subroutines.
Object-oriented programming on the other hand is a programming paradigm where computations are carried out on objects and data together. OOP makes use of classes to create new objects and instances.
One difference between OOP and procedural is that OOP uses principles like abstraction, inheritance, encapsulation and polymorphism, making it more secure than procedural.

Characteristics of Object Oriented Programming

  • Class

  • Object

  • Polymorphism

  • Inheritance

  • Encapsulation

  • Abstraction

Class

The building block that leads to Object-Oriented programming is a Class. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.

  • A Class is a user-defined data-type which has data members and member functions.

  • Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions define the properties and behaviour of the objects in a Class.

  • In the above example of class Car, the data member will be speed limit, mileage etc and member functions can apply brakes, increase speed etc.

class person //  person a class here
{
    char name[20];
    int id;
public:
    void getdetails(){}
};

Objects

An Object is an identifiable entity with some characteristics and behaviour. An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated. Object take up space in memory and have an associated address like a record in pascal or structure or union in C. When a program is executed the objects interact by sending messages to one another.

Each object contains data and code to manipulate the data. Objects can interact without having to know details of each other’s data or code, it is sufficient to know the type of message accepted and type of response returned by the objects.

class person
{
    char name[20];
    int id;
public:
    void getdetails(){}
};

int main()
{
person p1; // p1 is a object
}

Polymorphism

Polymorphism is a concept in object-oriented programming that refers to the ability of a single function or method to operate on multiple types of data. In other words, it allows objects of different types to be treated as objects of a common supertype. Polymorphism can be implemented in two main ways:

  1. Overriding: A subclass can provide a specific implementation of a method that is already defined in its superclass.

  2. Overloading: Different methods can have the same name but different parameters, in this way, it allows the same method name to be used for different types of data.

This allows for more flexible and reusable code, as objects can be manipulated without the need to know their exact type, only that they support certain methods or operations.

Polymorphism is one of the fundamental principles of object-oriented programming, along with inheritance and encapsulation.


Inheritance

Inheritance is a concept in object-oriented programming that allows one class to inherit properties and methods from another class. A class that is used as the basis for inheritance is called a superclass or base class, while a class that inherits from a superclass is called a subclass or derived class.

Inheritance allows for code reuse, as a subclass can inherit the properties and methods of its superclass, and can also add new properties and methods or override existing ones. This allows for a more organized and efficient use of code, as common properties and methods can be defined in a single location and then reused in multiple classes.

Inheritance can be implemented in different ways, such as single inheritance, multiple inheritance and multi-level inheritance. Single inheritance allows a class to inherit from only one superclass, while multiple inheritance allows a class to inherit from multiple superclasses. Multi-level inheritance allows a class to inherit from a class that inherits from another class.

Lightbox


Encapsulation

Encapsulation is a concept in object-oriented programming that refers to the practice of hiding the internal details of an object and exposing only the necessary information to the outside world. This is achieved through the use of access modifiers, such as private, protected, and public, which control the level of access to the properties and methods of an object.

By encapsulating the internal details of an object, the implementation of the object can be changed without affecting the code that uses the object. This makes the code more flexible and easier to maintain, as changes can be made to the implementation of an object without affecting the rest of the code.

Encapsulation also promotes information hiding, which is the practice of keeping the internal data of an object hidden from the outside world and providing a public interface to interact with the object. This helps to protect the internal data of an object from accidental or intentional modification, which can lead to unexpected behavior or errors.


Abstraction

Abstraction is a concept in object-oriented programming that refers to the process of reducing complexity by hiding unnecessary details and exposing only the essential information. It is closely related to encapsulation and information hiding.

Abstraction allows programmers to focus on the functionality of an object or system, rather than its internal details. It helps to simplify the design and implementation of complex systems by breaking them down into smaller, more manageable parts. This makes the code more modular, flexible and easy to maintain.

There are two types of abstraction in object-oriented programming:

  1. Abstract classes: They are classes that cannot be instantiated and are used as a base class for other classes to inherit from.

  2. Interfaces: They define a set of methods that a class must implement, but leave the implementation up to the class that implements the interface.