Coined by Alan Kay, object-oriented programming, also known as OOP or OO programming, is a programming language paradigm. In an object-oriented program, the code can be structured as reusable components, some of which may share properties or behaviors.
Object-oriented programming concepts
When programming in an object-oriented programming language, keep the following four key concepts in mind.
- Encapsulation - The grouping of related functions and data (variables) together into an object to help reduce complexity and allow parts of the code to be reused.
- Abstraction - Show only the essentials to reduce the abstract of change.
- Inheritance - Eliminate redundant code by inheriting functions and data from other classes.
- Polymorphism - Change how an object function depending on the data or class.
What is an OOPL?
An OOPL (object-oriented programming language) is a programming language that is based on the object-oriented programming model explained above. Examples of object-oriented programming languages are C++, C#, Java, Python, Simula, and Smalltalk.
General OOP example
Let's say you are writing a video game where the players control and race vehicles. If you are using an object-oriented programming language, you can define a class of objects named "vehicle." This class can contain definitions of the qualities and behaviors shared by all vehicles. For example, all vehicles accelerate and decelerate, and consume fuel. Within this class definition, you can define methods (similar to functions) called "accelerate" and "decelerate," and a property (a type of variable) called "fuel," whose value you can get or set.
Then, you could define subclasses, also called derived classes or child classes, that inherit methods and properties from the "vehicle" class. For example, you could define a subclass called "motorcycle" with two wheels, and a subclass called "car" with four wheels. Their shared qualities (acceleration, deceleration, and fuel consumption) are already defined in "vehicle," so you don't need to write those again.
Then, you could instantiate these subclasses to define individual playable vehicles. Each instance of the subclass would inherit methods and properties from all its parent classes, and have properties and methods of its own. For example, its unique properties might include paint color ("red," "blue," etc.), and price (if the player wants to purchase it with in-game currency).
Later, when you want to change the code that is specific to a motorcycle, you don't need to change the parent "vehicle" class. The "vehicle" code has already been tested, and works fine, so modifying your program requires less work. Additionally, because fewer lines of code are being modified, there is a reduced chance that a bug will be introduced somewhere in the new code. If a bug does appear, you don't need to wonder if it's in the "vehicle" class code, because that class was not modified. Development and maintenance of the software may therefore be more efficient, saving time and effort.