Skip to content

Object-Oriented Programming (OOP)

Overview

Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to represent data and methods to manipulate that data. OOP is designed to increase the modularity, reusability, and maintainability of code by organizing it into classes and objects.

Principles of OOP

Encapsulation

Encapsulation is the principle of bundling data (attributes) and methods (functions) that operate on the data into a single unit known as a class. This helps to restrict direct access to some of an object's components, which can prevent the accidental modification of data. Access to the data is typically controlled through public methods known as getters and setters.

Inheritance

Inheritance is a mechanism by which a new class, known as a subclass or derived class, can inherit attributes and methods from an existing class, known as a superclass or base class. This promotes code reusability and establishes a hierarchical relationship between classes.

Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables a single interface to represent different underlying forms (data types). The most common use of polymorphism is when a method in a subclass overrides a method in its superclass.

Abstraction

Abstraction is the concept of hiding the complex implementation details and showing only the essential features of an object. It helps to reduce complexity and increase efficiency by allowing the user to interact with the object at a higher level without needing to understand its internal workings.