Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is a programming paradigm (a style of writing code) based on the concept of objects which can contain data and code. The data is represented as fields (often called attributes or properties), and the code is represented as procedures (often called methods). Objects are instances of classes, which act as blueprints for creating objects.
It majorly consists of two things:
- Class: A class is a blueprint or template that defines the properties (attributes) and behaviors (methods) common to all objects of its type.
- Object: An object is an instance of a class, representing a specific entity with its own unique state (attribute values) and behavior.
Difference between Procedural programming and Object Oriented programming
There are two major programming paradigms which are: Procedural programming and object-oriented programming. Both of these differ from each other in various aspects:
- Approach:
- Procedural programming focuses on a step-by-step sequence of actions to perform tasks where the control of the program moves in a sequential manner.
- OOP focuses on modeling real-world entities as objects, combining data (attributes) and behavior (methods) without a particular flow of control.
- Data Handling:
- Data is globally accessible in Procedural programming which can lead to accidental modifications and harder-to-maintain programs.
- In OOP, data is encapsulated within objects, and access is restricted through methods. This ensures better data security, as only authorized methods can modify the data.
- Code Reusability:
- Code reusability is limited in procedural programming. Functions can be reused, but there is no concept of inheritance or polymorphism to extend and customize functionality easily.
- Code reusability is high due to features like inheritance (reusing code in child classes) and polymorphism (handling objects in a generic way for flexibility).
- Scalability:
- Procedural code is often harder to scale for complex projects because adding new functionality requires modifying multiple functions and potentially breaking existing code.
- OOP scales better for larger systems. Adding new functionality typically involves creating new classes or modifying existing ones with minimal impact on unrelated parts.
- Modularity:
- In procedural programming, programs are broken into functions, but the separation of logic and data is less structured, making large programs harder to manage.
- In OOP, programs are modularized into classes and objects, ensuring better organization, maintainability, and scalability for large and complex systems.
- Real-World Modeling:
- Procedural programming is less aligned with real-world systems, as it focuses on actions and lacks an intuitive way to represent real-world entities or relationships.
- OOP closely mirrors real-world scenarios by representing entities as objects with attributes and behaviors, making it easier to design and understand complex systems.
Use-Cases of Object Oriented Programming