Object Oriented Programming
Definition
Object Oriented Programming is a programming paradigm in which a program is organized into classes and objects, where objects contain attributes (data) and methods (functions). It supports core principles such as encapsulation, inheritance, polymorphism, and abstraction, which help in building modular, reusable, and maintainable software.
Main Content
1. Class and Object
Class
- : A class is a blueprint or template used to create objects. It defines what attributes and methods the objects will have. For example, a
Studentclass may define attributes likename,rollNumber, andgrade, and methods likestudy()ortakeExam().
Object
- : An object is an instance of a class. It is a real usable entity created from the class blueprint. For example,
student1andstudent2can be objects of theStudentclass, each having different values for name and roll number.
A class does not occupy memory until objects are created from it, whereas objects are actual entities that store data in memory.
Example:
Class: Car
Attributes: color, brand, model
Methods: start(), stop(), accelerate()
Object 1: myCar = Car("Red", "Toyota", "Corolla")
Object 2: yourCar = Car("Blue", "Honda", "Civic")
Real-world idea:
A class is like a building plan, and an object is the actual building constructed from that plan.
2. Encapsulation, Inheritance, and Polymorphism
Encapsulation
- : Encapsulation means wrapping data and methods together into a single unit called a class and restricting direct access to some of the object's internal details. This is usually done using access modifiers such as
private,protected, andpublic. It protects data from accidental misuse and keeps the internal state safe.
Example: A bank account balance should not be changed directly by outside code. Instead, a method like deposit() or withdraw() should control how the balance changes.
Inheritance
- : Inheritance allows a new class to acquire the properties and methods of an existing class. The existing class is called the parent class or base class, and the new class is called the child class or derived class. This supports code reuse and establishes an "is-a" relationship.
Example: A Dog class can inherit from an Animal class. The Dog class automatically gets common features like eat() and sleep(), while also defining its own behavior like bark().
Polymorphism
- : Polymorphism means "many forms." It allows one interface or method name to behave differently depending on the object that uses it. This makes programs flexible and easier to extend. Polymorphism is commonly achieved through method overloading and method overriding.
Example: A draw() method may behave differently for a Circle, Rectangle, and Triangle.
Simple relationship diagram:
Class 1 ---> Object 1
Class 1 ---> Object 2
Animal ---> Dog
Animal ---> Cat
draw() ---> Circle version
draw() ---> Rectangle version
draw() ---> Triangle version
These three principles are foundational to OOP and are often used together in real software systems.
3. Abstraction and Modular Design
Abstraction
- : Abstraction means hiding unnecessary implementation details and showing only the essential features of an object. It helps programmers focus on what an object does rather than how it does it. In OOP, abstraction can be achieved using abstract classes, interfaces, or well-designed class structures.
Example: When driving a car, you use the steering wheel, accelerator, and brake without needing to know the internal engine processes.
Modular Design
- : OOP encourages breaking a large program into smaller, manageable modules such as classes and objects. Each class handles a specific responsibility. This makes debugging, testing, and updating software much easier.
Example: In an e-commerce system, you may have separate classes for Customer, Product, Order, Payment, and Shipping. Each class manages its own part of the system.
Code Reusability and Maintenance
- : OOP promotes reusing existing code through inheritance and composition. It also makes maintenance simpler because changes in one part of the code are less likely to affect unrelated parts of the system.
Example scenario:
If a university system has a Person class, then Student and Teacher classes can reuse common features like name and age while adding their own specific behavior.
This concept is especially important in large applications where clean design reduces complexity and improves long-term support.
Working / Process
1. Design the classes
- Identify the real-world entities that need to be represented in the program.
- Decide what attributes and methods each class should have.
- For example, in a library system, classes may include
Book,Member,Librarian, andLoan.
2. Create objects from the classes
- Once the class blueprint is ready, objects are created during program execution.
- Each object gets its own set of data.
- For example,
book1may represent "Mathematics", whilebook2may represent "Science".
3. Use object interactions to perform tasks
- Objects communicate with one another by calling methods.
- The program’s logic is achieved through these interactions rather than a single long procedure.
- For example, a
Customerobject places an order, anOrderobject calculates total cost, and aPaymentobject processes the payment.
Process illustration:
Real-world entity
↓
Class design
↓
Object creation
↓
Method calls / interaction
↓
Program output
This workflow makes OOP highly suitable for structured and scalable software development.
Advantages / Applications
Improved code reusability and reduced duplication
Through inheritance, existing code can be reused in new classes. This reduces repetition and saves development time.
Better organization and maintainability
Programs built using OOP are divided into logical components, making them easier to read, test, debug, and update.
Used in real-world software systems
OOP is widely used in desktop applications, mobile apps, web applications, game development, banking systems, e-commerce platforms, simulation software, and enterprise applications.
Additional application examples:
Banking systems
- :
Account,Customer,Transaction
School management systems
- :
Student,Teacher,Course
Games
- :
Player,Enemy,Weapon,Level
Hospital systems
- :
Doctor,Patient,Appointment
OOP is especially valuable when software must grow over time and support many features without becoming unmanageable.
Summary
- Object oriented programming is a way of designing software using classes and objects.
- It helps organize code around real-world entities and their behavior.
- Core OOP ideas include encapsulation, inheritance, polymorphism, and abstraction.
- Important terms to remember: class, object, attribute, method, encapsulation, inheritance, polymorphism, abstraction