OOP or Object Oriented Programming is a method of programming that sees software entities as objects. Before the advent of object-oriented programming, developers were often writing procedurally. In procedural programming, the software was divided into functions. That means the program consists of variables that store information and functions that perform the desired operation.
However, as software grew, programmers had to repeat codes over and over again. The different parts called each other unevenly and intricately, leading the program to become a “Spaghetti code.” Spaghetti code refers to code in which various functions and grouped elements are interconnected in a disorderly manner, and as a result, changing in a function causes many others to crash.
After the advent of object-oriented programming, related functions and variables were into units called classes. Objects are built of these classes and get the methods and attributes from them. Classes can also inherit properties or functions from each other. This feature is called Inheritance.
Take your phone, for example. Suppose your phone model is SumsungS10. The SumsungS10 class inherits from the phone class. The phone class has the following features or attributes:
- Color
- Resolution
- Camera resolution
- Memory
It also has the following functions as an example:
- Charging
- Take photos
- Receive SMS
The following code displays an empty cell phone class:
Now with the Extends keyword, the features and methods of the mobile phone class will be inherited by the SumsungS10 class:
The SumsungS10 phone class receives all of these features and functions by inheriting from the phone class. It also has a Gadget feature. Your SumsungS10 is an instance of the SumsungS10 class created by generating an object of the SumsungS10 class.
Object-oriented programming is like everyday life.
Object-Oriented Programming is, in fact, a type of logic or pattern derived from our daily lives that originated around the 1960s. In object-oriented programming, based on object modeling, we see the program as similar to the world around us and our daily lives.
When you want to use something in everyday life, you do not need to know how it works. For example, an espresso machine is quite sophisticated, but you do not need to know how it works. All you need to know is that you will receive an espresso when you press the power button.
The same is true of object-oriented programming. In the example of chess, we can have a move () method. This method may require large amounts of data and other methods. It may also require variables such as the initial and final position of the chess piece.
But you do not need to know this, and all you need to know is that when we want a bead to move, it moves.
The realm of variables in object orientation
As you can see in the code above, we used the words public, protected, and private to define attributes and methods. Using these keywords, we describe the domain of class attributes and methods. These keywords are called Access Modifier.
In summary:
- Public: Variables and methods that are accessible from outside the class.
- Protected: Variables and methods that are only accessible for classes inherited from the current one.
- Private: Variables and methods that are not accessible from outside the class.
Object-Oriented Programming Features:
- Abstraction: means ignoring aspects that we do not currently need. We can also get an overview of the issue without going into detail, just like a city and country map.
- Enclosure: The object attributes and information are hidden from other objects, and they only communicate through the messages exchanged.
- Inheritance: Inheritance is to express similarities. To avoid duplication of common features, the programmer builds a class. Attributes are inherited to all instances made from one class.
- Reusability: Reusability is a feature that causes the characteristics of the parent class to be expanded or reused. C ++ features provide reusability.
What are the benefits of object-oriented programming?
- Increase application security
- Reduce maintenance costs
- Reusability
- Easier program analysis
- Ability to organize codes more optimally
- No need to write duplicate code and features that have already been implemented and save resources
- Ability to split the program into smaller but standalone applications
Keep in mind that in this article, we have only discussed the generalities of object-oriented programming. Each programming language has its own methods and arrangements. Once you have chosen the right language, you can start studying its uses in object-oriented concepts. And last but not least, one of the design patterns used to solve this type of programming problem is the Singleton Design Pattern.
Learn more: To learn more about Singleton, read Singleton Design Pattern With a Practical Example.