Below you’ll find some of the basic concepts of Object Oriented Programming:
OOP: Object Oriented Programming is a software design philosophy or approach, which aims to model software closer to what we see in real life. While procedural languages like C focus on functions that manipulate data, OOP languages like C++ or Java focus on classes of objects that have specific behaviors. The main advantages of the OOP approach are:
- Modularity: each class/object can be developed independently of the others.
- Information hiding: objects don’t need to know how other objects work or what attributes they have.
- Code re-use: once a class/object is working it can be re-used on different parts of the program and even or different programs.
- Maintainability: the three points above make the process of developing and maintaining large software systems a lot easier.
Objects: As the name implies, objects are a central part of OOP. As humans we see the world in terms of objects: cars, tables, books, animals, other humans, etc. The main idea behind OOP is to model software in terms of those same objects. Each object has certain attributes, states and behaviors. Let’s consider a lamp, for instance. Its attributes can be its size and brightness. Its states are only two: it can be on and it can be off. Its behaviors are also only two: it can be turned on (in case it’s off) and it can be turned off (in case it’s on).
Classes: A class is a blueprint from which objects of that class will be created. For instance, there is a class for cars, and each car is an instance of that class. Classes need to describe both the attributes/states of the object as well as the behaviors.
Encapsulation: This principle is closely related to the description of object above. It basically says that software objects should include both their attributes/states and their behaviors. The attributes/states are stored in fields/variables, while the behaviors are stored in methods/functions, and everything is encapsulated together. Another aspect of encapsulation is that one object cannot see the attributes/states of another object, and communication/interaction is only possible via specific methods that provide the interface. This leads to information hiding and modularity, which are desired traits in software development.
Inheritance: A class can be derived from previously existing class, in which case it will inherit some or all of its attributes/states and behaviors. For instance, sports cars will have most of the characteristics of normal cars (e.g., 4 wheels, accelerator, brakes, etc), but they might come with new attributes and behaviors (e.g., spoilers, turbo acceleration, etc).
Polymorphism: Formally called subtype polymorphism, this is the ability to make variables, objects and functions have more than one form, and there’s a common interface for manipulating all of them. For example, numeric variables can hold ints, floats and doubles, and the operators and functions to manipulate them tend to be the same ones.
Public/Private: Most OOP languages use public and private labels inside its classes to specify which methods and attributes of an object can be accessed by external functions and which can’t. This is a fundamental feature to implement encapsulation and information hiding.