In programming, OOP is an acronym for Object-Oriented Programming. As you have read before, this is the core of the Java language; this is the good stuff.
Why Use OOP?
Now, knowing many of the fundamentals of programming with Java, such as data types, variables, operators, conditionals, arrays, and so on, you are now poised to start creating very interesting applications that tackle real-world challenges using what is known as Object-Oriented Programming.
Object-oriented programming is just a method of representing data in a logical manner. Software deals in data. We constantly need to parse data (make sense of it), analyze data, update data, store data, and so on. So, how do you deal with and make sense of all this data? Well, that's where OOP comes in. There are many programming paradigms that stand alongside OOP.
Different Programming Paradigms
Some examples of programming paradigms that aren't OOP include
There are many others; like tools in a toolkit, they all have purposes for which they are best suited.
Why is OOP So Commonly Used?
Object-oriented programming has been and continues to be one of the best and most regularly used tools in the toolkit. This is because it allows us to model data and the applications that act on that data in a very intuitive way. You can model the world around us, whatever that may be, using Object-Oriented Programming. Then, once you've modeled the required data (classes/objects) required by the application, you can interact with that data in a very logical way.
As you've been working through the previous sections on classes and objects, you might have been already starting to see how this all works. For instance, if you're building an application that manages a restaurant you'd have classes such as Restaurant, Menu, MenuItem, Beverage, Employee, Guest and so on. Then, once you've got these classes that map out the application, you can quickly associate, compare, update, collate, analyze, and so on with all the data in that model. As you'll see later, this comes in handy because you can also map these objects to databases, and communicate with other services on the web using these objects (in the form of JSON).
The Four Primary Components of OOP
In the context of what you already know about classes, you'll be introduced to the four concepts that dictate how you best use them.
1. Abstraction
Abstraction is the idea of showing only the necessary and essential features of a system to the user, thus hiding the details and complexity that do not add a benefit to the user.
An example of abstraction in your everyday life could be a car: as a driver, you're made aware of the steering wheel, the pedals, the dashboard, and other functionalities inside the car that enable you to use it. However, the workings of the engine, the transmission, and other aspects of the car are abstracted from you because they are not essential to being able to drive the machine.
2. Encapsulation
Encapsulation is the idea of grouping similar data and methods together inside a capsule (properly referred to as a class), which helps to organize your program as well as control access to this information. You have already been introduced to this idea since every class and object that you've seen so far is an example of encapsulation.
3. Inheritance
Inheritance is when one class (considered a "child class") can inherit the variables and methods of another class - often referred to as the "parent class". This process reduces the amount of code in your program since shared attributes between objects can be stored in one place: their parent class.
4. Polymorphism
Polymorphism essentially means multiple forms, and in programming, this provides the ability to have what appears to be a single function or behavior act in a variety of different ways. This one is always the trickiest to wrap your head around first, but you were introduced to this idea in the method overloading course. As a quick refresher, you saw that System.println() is a common example of polymorphism since you can send it any number of arguments of any type.
In the upcoming section, you'll discuss each of these in detail.
Summary: What is OOP
- OOP stands for Object Oriented Programming
- OOP is one of many programming paradigms, where some examples include
- Functional
- Procedural
- Imperative
- Declarative
- OOP is the core of the Java language
- OOP is an excellent way to model real-world data
Four Components of OOP
- Abstraction: showing only essential functionalities
- Encapsulation: grouping similar data and methods together
- Inheritance: sharing common data and methods between objects with a parent
class - Polymorphism: allowing functions and behaviors to have multiple forms