What is Object-Oriented Programming (OOP)?

Learn via video courses
Topics Covered

We can say that the OOP (Object Oriented Programming) is a programming paradigm or concept that aims to implement real-world objects. If you can identify the objects in your code which are similar to real life objects and can structure your code accordingly in classes and objects then you can say that OOP principles are used in your code. So classes and Objects are the basic building blocks of the OOP concept. C++, Java, Javascript are major Object Oriented languages.

object oriented programming

OOPs concepts

object oriented programming concepts

These are basic concepts of OOPS. We will take a deeper look at each one:

Object

As described at the starting of the article, Objects are real-world entities that contain specific data or information, so you can say that objects are instances of classes that contain specific data or information related to that object. For example, we have defined the “Room” class to represent a hotel room. So to create an instance or object of the “Room” class you have to define an object for the “Room” class which represents a specific room of the hotel. In C++ to define object syntax is as follows:

Here r1 is an instance or object of class Room and will have its specific room_number, room_rent, room_status.

Object in OOP

Note: Whenever you define a class, only the properties of the object are defined, no memory is allocated. So to allocate memory and to use data and functions of a class you have to create objects of that class.

Class

A class can be considered as a blueprint or template for creating similar types of objects. Classes are a user-defined data type that defines two things:

  • Attributes or data
  • Behavior or methods

Individual objects are then created using the class or blueprint.

For example, in the hotel management system, you can create a class “Room” to define each room in the hotel which will have attributes and behavior of each room. So room class can contain the following fields or attributes:

Class in OOP

In C++ class is defined using the “class” keyword and then its name is there followed by the body of class in curly braces ending with a semicolon. So to represent class for a room, here is the syntax:

The body of the class will contain methods and data related to Room.

Refer to this link for more information about class: Class in OOPS

Inheritance

Inheritance promotes the reusability of code. If you want to create a new class and that class has some fields or code common to another class, you can use that class as the parent class and can inherit that class. So in this way, you don’t have to write that same piece of code again and can use that code in the child class or class which you are creating. Hence that code is reused.

The syntax for inheritance is “extends” keyword is used to inherit another class.

Inheritance in OOP

Polymorphism

Polymorphism means one thing has different forms. Polymorphism allows a member function of a class to behave differently based on the object that will call it. Polymorphism occurs when classes are related through inheritance.

Polymorphism in oop

Polymorphism can be achieved through function overloading and function overriding.

  • Function overriding: In function overriding child class can exhibit different implementation of the same function which has been defined in parent class also, according to its usages.

The above example of “Instrument” class is an example of Function overriding.

  • Function overloading: Polymorphism is also achieved through function overloading. In function overloading, methods can have the same name but the number of arguments is different in function calls. Results will be different according to the number of arguments with which the function is called.

findArea(3) call will go to 1 function as the number of arguments in function is one and findArea(2,5) call will go to 2 function as the number of arguments in the function call is two. This is the concept of function overloading.

Refer to this link for more information about Polymorphism: Polymorphism in OOPS

Abstraction

Abstraction involves showing only necessary details to the user and hiding irrelevant details or unnecessary details from the user. It is an extended form of Encapsulation and also serves a security purpose. As complex code is hidden from the user so it becomes easier for the user to use that software and also that software is easier to maintain.

inheritance oop

Refer to this link for more information about Abstraction: Abstraction in OOPS

Encapsulation

Encapsulation is a mechanism in which attributes and behaviors are bound together in a class. You can think of Encapsulation as just like a medicine capsule in which many medicines are combined or bound together. Similarly, encapsulation binds data and methods in a class together. Also, Encapsulation provides a function of a secure layer by hiding the software or product internal implementation of code and internal data of the class and exposing only required information to the outer world.

encapsulation in OOP

In encapsulation, the programmer requires to define some fields or data as public or private:

Public: These methods or variables(fields) are accessible both from the same class as well as from outer classes also.

Private: These methods or variables(fields) are accessible only in the same class.

Here we are making the room_rent variable or field private so it can only be changed by the function of the same class. As we are hiding data from outer classes or the world, Encapsulation is also known as Data hiding.

Refer to this link for more information about Encapsulation: Encapsulation in OOPS

Main principles of OOP

Object-oriented programming (OOP) is built on four main principles, often referred to as the "four pillars of OOP." These principles provide a framework for designing and organizing code in a way that promotes modularity, reusability, and maintainability. The four main principles of OOP are:

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

List of Object-Oriented Programming Languages

Many programming languages support object-oriented programming (OOP) principles to varying degrees. Here is a list of some popular object-oriented programming languages:

  1. Java
  2. C++
  3. C#
  4. Python
  5. Ruby
  6. JavaScript
  7. Objective-C
  8. Swift
  9. PHP
  10. Scala
  11. Kotlin
  12. Smalltalk

Benefits of OOP:

  • OOP concept allows breaking the project or software into smaller modules or chunks so it makes debugging or testing easy for programmers.
  • OOP promotes the reusability of code. For example, using inheritance you can use the code of the parent class in the child class and can eliminate the duplicate code.
  • OOP systems or software can be scaled easily.
  • As OOP provides the concept of data hiding, it allows programmers to develop secure programs or projects in which any unauthorized user can’t invade or attack.
  • OOP concept provides the flexibility in writing code using polymorphism, as you can use the same name function to provide its implementation according to your usage or requirements.
  • OOP systems are easily maintainable.

Disadvantages of OOP

  1. Steep Learning Curve: OOP introduces new concepts like classes, objects, inheritance, and polymorphism, which can be challenging to grasp, especially for beginners.

  2. Performance Overhead: OOP programs tend to be larger and more complex than procedural programs due to the introduction of objects and their associated data and methods.

  3. Memory Consumption: Object-oriented designs often involve the creation of multiple objects, each requiring memory allocation.

  4. Overuse of Inheritance and Polymorphism: While inheritance and polymorphism are powerful features of OOP, they can be overused or misused, leading to overly complex and difficult-to-maintain code.

Applications of OOPs

  1. Computer graphics applications
  2. Object-oriented database
  3. User-interface design such as windows
  4. Real-time systems
  5. Simulation and modeling
  6. Client-Server System
  7. Artificial Intelligence System

Conclusion

  1. OOP is a powerful and versatile programming paradigm that offers many benefits for developing modern software applications.
  2. OOP promotes code reusability, maintainability, and extensibility.
  3. OOP principles are based on the concept of objects, which represent real-world entities, making it easier to model complex systems.
  4. OOP has a steep learning curve and can lead to performance overhead and memory consumption if not used properly.
  5. OOP is not suitable for all problems and should be considered carefully when choosing a programming approach.

See Also

Four Pillars of OOPs