|
What is
Object Oriented Programming (OOP)?
Traditional programming languages (C, COBOL, FORTRAN...) follow a structured programming
model. In this model, data is separately stored and procedural code processes data.
For example, in C, data is stored in structures and processing is done by functions.
When used on large projects, this type of programming revealed several weaknesses:
1. Not modular: Any of the data is accessible from any piece of code. This made it
difficult to demarcate the areas for different teams working on a large project.
2. Not easy to fix: When we go back and make a change in one part of the program, it is
difficult to foresee and contain its impact from other parts of the program.
3. Not enough code reuse: Even though function and subroutine libraries were built to
promote reuse of code, it was obvious that a way was needed to reuse larger chunks of
code.
OOP came in to solve some of the above problems. OOP bundles data and the processes which
act on that data into a single unit called an object. Objects have properties to store
data and methods to process that data, all bundled into a single unit.
Think of structured programming as a
centralized library where any employee can get and replace books in any section. OO is
like a decentralized library where one employee is in charge of 'fiction', another is in
charge of 'arts' and so on. If employees in charge of one section want a book from
another, they can't just go and grab it, they have to ask.
What are the benefits of
OOP?
Encapsulation: Objects hold together well demarcated units of functionality. They hold all
data and processing pertaining to that functionality together. Objects are accessed
through their public interfaces by sending messages. No need to read all the
implementation code to check usability.
Code reuse: In structured programming only processing can be reused. In OOP, inheritance