Programming Concepts: Object-oriented programming (OOP)
Where Procedure-oriented programming uses procedures to make code easier to write and understand, Object-oriented programming (OOP) goes a step further and uses objects to make code easier to create and work with.
In OOP, the word "object" has special meaning: objects are defined as a specific way of organizing source code. Briefly, an Object is the combination of:
- Some data (i.e. variables) that are related to each other
- Procedures specifically designed to work with that data
A car could be represented by a Car object.
If a program only requires a few lines of source code to write down, there is no advantage to using this technique. Procedure-oriented and Object-oriented programming were invented because programs were getting longer and longer, and were difficult to work with. Programmers needed more structure to simplify the programming process.
A program of moderate size and complexity can be simplified using procedures. With especially large or complex programs, procedures are not enough; OOP became popular as a way of handling these very complex programs.
A program may be complex in the raw source code - many lines of code, many procedures. Or it may be complex in the way it's written - many human authors, many interactions between different parts. OOP helps with both.
We will look at the detailed reasons for and against once we've covered the key concepts.
Improvements to Procedure-Oriented Programming
OOP started from observing ways of writing Procedure-oriented programs that were more effective (easier to write, had fewer bugs) than others. A few techniques that were optional in Procedure-oriented programming became the core of OOP:
- Encapsulation - a programming language mechanism for restricting direct access for IT-developers to some of the object's data. The object Student can have public variable knowlage_lavel and private variable critical_information).
- Aggregation - an aggregate object is an object which contains other objects. For example, a Car class would contain Engine, Wheels, Cabin, Fuel objects. Sometimes a class refers to real-world physical objects (like a Car). Sometimes it is more abstract (e.g. university and students).
- Composition - composition is like aggregation but it is real-time technique. Using interfaces you can replace objects at real time. But objects must have the same type.
- Interfaces - the Interface separates the implementation and defines the structure. It's like car's digital-audio panel - where you can choose between rock, classical music or for example choose star-fm radio station. This concept is useful when implementation of the object can be interchangeable. Also, please pay attention that you can use this technique if implementation is changing frequently!