DEV Community

Sagar Dutta
Sagar Dutta

Posted on

Object Oriented Programming

Introduction

OOPs stands for Object-Oriented Programming, which is a programming paradigm that uses objects and classes to represent real-world entities and their behaviors.

The main concept of OOPs is to bind the data and the functions that work on that together as a single unit so that no other part of the code can access this data.

Some of the major OOPs concepts are:

Class

A class is a blueprint or a prototype for creating objects. It defines the attributes and methods that an object can have. For example, a class named Parrot can have attributes like name, age, color and methods like fly, sing, etc.

# Class
class Parrot:
    # Class attribute
    species = "bird"

    # Instance attribute
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # Instance method
    def sing(self, song):
        print(f"{self.name} sings {song}")
Enter fullscreen mode Exit fullscreen mode

Object

An object is an instance of a class. It has a state (attributes) and a behavior (methods) associated with it. For example, an object named blu can be created from the Parrot class with specific values for its attributes and methods.

# Object
blu = Parrot("Blu", 10)
print(blu.species) # bird
print(blu.name) # Blu
print(blu.age) # 10
blu.sing("Happy") # Blu sings Happy
Enter fullscreen mode Exit fullscreen mode

Inheritance

Inheritance is a way of creating a new class from an existing class without modifying it. The new class is called a derived class or a child class, and the existing class is called a base class or a parent class. The derived class inherits the attributes and methods of the base class and can also add its own features. For example, a class named Dog can inherit from a class named Animal and add its own method named bark.

# Inheritance
class Animal:
    def eat(self):
        print("I can eat")

    def sleep(self):
        print("I can sleep")

class Dog(Animal):
    def bark(self):
        print("I can bark")

dog1 = Dog()
dog1.eat() # I can eat
dog1.sleep() # I can sleep
dog1.bark() # I can bark
Enter fullscreen mode Exit fullscreen mode

Polymorphism

Polymorphism means having many forms. In OOPs, it refers to the ability of an object to behave differently depending on the context. For example, a method named sound can have different implementations for different classes like Dog, Cat, Bird, etc. Polymorphism can be achieved by using method overriding (redefining a method in the derived class) or method overloading (defining multiple methods with the same name but different parameters).

# Polymorphism
class Bird:
    def sound(self):
        print("I make some sound")

class Duck(Bird):
    def sound(self): # Method overriding
        print("I quack")

class Owl(Bird):
    def sound(self): # Method overriding
        print("I hoot")

duck1 = Duck()
owl1 = Owl()
duck1.sound() # I quack
owl1.sound() # I hoot
Enter fullscreen mode Exit fullscreen mode

Encapsulation

Encapsulation is the process of hiding the internal details of an object from the outside world. It prevents other classes from accessing and modifying the attributes and methods of a class. Encapsulation can be achieved by using private attributes (denoted by single _ or double __ prefix) and setter and getter methods (to access and modify private attributes). For example, a class named Computer can have a private attribute named __maxprice and a setter method named setMaxPrice to change its value.

Abstraction

Abstraction is the process of hiding the unnecessary details of an object and exposing only the essential features. It helps to reduce complexity and increase efficiency. Abstraction can be achieved by using abstract classes (classes that cannot be instantiated and have abstract methods that must be implemented by the derived classes) or interfaces (classes that only have abstract methods and constants). For example, an abstract class named Shape can have an abstract method named area that must be implemented by its subclasses like Circle, Square, Triangle, etc.

Some of the benefits of OOPs are:

  • Reusability

    It means reusing some facilities rather than building them again and again. This is done with the use of classes and inheritance. We can use a class or a subclass ā€˜nā€™ number of times as per our need.

  • Data Redundancy

    It is a condition where the same piece of data is held in two separate places. OOPs helps to prevent data redundancy by using classes and objects. If a user wants a similar functionality in multiple classes, he/she can write a common class definition for that functionality and inherit it.

  • Code Maintenance

    This feature helps users to avoid re-work and save time. It is always easy and time-saving to maintain and modify the existing code by incorporating new changes into it. OOPs supports code maintenance by using encapsulation, polymorphism and abstraction mechanisms.

  • Security

    With the use of data hiding and abstraction, we are filtering out limited data to exposure, which means we are maintaining security and providing necessary data to view. OOPs also helps to protect the data from unauthorized access by using private attributes and methods.

  • Design Benefits

    OOPs forces the designers to have a long and extensive design phase, which results in better designs and fewer flaws. It also helps to reduce complexity and increase efficiency by using abstract classes and interfaces. OOPs makes it easier to program all the non-OOPs one separately.

  • Better Productivity

    With the above-mentioned benefits, OOPs enhances the overall productivity of the users. It helps to develop faster, cheaper and quality software that meets the user requirements.

Some of the disadvantages of OOPs are:

  • Complexity

    It can be challenging to design a program with an OOP concept. A program is developed in OOP if there is a plan for it. The programmer needs to have brilliant designing skill and programming skill along with proper planning. The thought process involved in OOP may not be natural for some people.

  • Size

    The size of programs that have been developed with OOP is bigger than those that have a procedural approach. This is because OOP uses classes, objects, inheritance, polymorphism, etc. that add extra code and data to the program.

  • Execution time

    Execution time for OOP programs is higher since they are bigger. They also involve more memory allocation and deallocation operations due to the use of objects. OOP programs may not be suitable for time-sensitive applications that require fast performance.

  • Applicability

    OOP is not a universal language. It is applied only when it is required. It is not suitable for all types of problems. Some problems may be better solved by using other programming paradigms like functional, procedural, or logical programming.

Top comments (0)