Object-Oriented Programming (OOP) is a programming paradigm used to design programmes in a modular and object-oriented way.
It provides a set of programming rules called the ‘Object Oriented Programming paradigm’, which is utilised for the designing and programming of a program. Object-Oriented Programming divides the application program into classes (objects). Everything is a self-contained unit that holds specific properties, as well as methods for manipulating those properties.
In the world around you, you interact in an object-oriented environment. To go shopping, for example, you interact with a car object. An automobile comprises several things that work together to transport you to the destination. Put the key in the ignition and spin it. An electrical signal is sent to the starting item, which interacts with the engine to start the automobile.
As a driver, you are isolated from the logic of how the system’s objects work together to start the car. You Initiate the sequence of events by executing the start method of the ignition object with the key. You then wait for a response (message) of success or failure. Similarly, users of software programs are isolated from the logic needed to accomplish a task. For example, when you print a page in your word processor, you initiate the action by clicking a print button. You are isolated from the internal processing that needs to occur; you wait for a response telling you if It is printed.
In the software program, the button object interacts with a printer object, which interacts with the actual printer to accomplish the task of printing the page.
The Characteristics of OOP
This section will provide some basic concepts and words common to all 001′ languages. Don’t worry about implementing these ideas in a specific programming language. I hope you will understand the ideas better as you learn more about OOP design and implementation.
We live in an object-oriented world. You’re a thing. You can interact with objects. You are an object with data like height and hair colour. You also have techniques you do or are done to you, like eating and walking. So, objects? An object in OOP is a structure for storing and manipulating data.
For example, say you wanted to track product inventory data. You’d construct a product object to manage and use product data. If you want your app to print, you work with a printer object that contains the data and methods required to communicate with your printers.
When you interact with objects in the world, you are often only concerned with a subset of their properties, without this ability to abstract or filter out the extraneous properties of objects. You will find it hard to process the plethora of information bombarding you and concentrate on the task.
Due to abstraction, two individuals dealing with the same object typically deal with distinct subsets of characteristics. Like when I drive my vehicle. The car’s speed and direction must be known. Because the automobile has an automatic gearbox, I don’t need to see the engine’s RPM.
On the other hand, this information would be critical to a mutant driver, who would not filter It out. When constructing objects in OOP applications, It Is vital to Incorporate this concept of abstraction.
The objects include only the relevant information in the context of the application. If you were building a shipping application, you would construct a product object with attributes such as size and weight. The colour of the Item would be extraneous information and would be ignored. On the other hand, when constructing the order-entry application, the colour could be essential and included as an attribute of the product object.
Another important feature of OOP is encapsulation. Encapsulation is the process in which no direct access is granted to the data; instead, it is hidden. To gain access to the data, you have to interact with the object responsible for the data. In the previous inventory example, if you wanted to view or update information on the products, you would have to work through the product object.
To read the data, you would send the product object a message. The product object would then read the value and send back a note telling you what the value is. The product object defines which operations can be performed on the product data. If you send a message to modify the data and the product object determines it is a valid request, it will operate for you and send a message back with the result. You experience encapsulation in your daily life all the time.
Consider an HR department. They enclose (conceal) personnel data. They decide how to utilise and modify data. Any requests for personnel data or updates must go via them. Network security is another. A network security administrator must be contacted for security information or policy changes.
The network users’ security data is encapsulated. Encapsulating data makes your system’s data safer and more dependable. You understand how data is accessed and what actions are taken on it. This facilitates software maintenance and troubleshooting. You may also change the methods used to work with the data, but you don’t have to change the other objects that utilise the method.
Polymorphism is the ability of two different objects to respond to the same request message in their own unique way. For example, I could train my dog to respond to the command bark and my bird to respond to the command chirp. On the other hand, I could train them to respond to the command “speak.” Through polymorphism, I know that the dog will respond with a bark, and the bird will respond with a chirp. How does this relate to OOP?
You can create objects that respond to the same message in their unique implementations. For example, you could send a print message to a printer object that would print the text on a printer, and you could send the same message to a screen object that would print the text to a window on your computer screen.
Another good example of polymorphism is the use of words in the English language. Words have many different meanings, but through the context of the sentence, you can deduce which meaning is intended. You know someone who says, “Give me a break!” is not asking you to break his leg! In OOP, you implement this type of polymorphism through overloading. You can implement different methods of an object that have the same name.
The object can then tell which method to implement depending on the message’s context (in other words, the number and type of arguments passed). For example, you could create two methods of an inventory object to determine a product’s price. Both these methods would be named getPrice. Another object could call this method and pass either the product name or the product ID.
The inventory object could tell which getPrice method to run by whether a string value or an integer value was passed with the request.
Most real-life objects can be classified into hierarchies. For example, you can classify all dogs as having certain common characteristics, such as four legs and fur. Their breeds further classify them into subgroups with common attributes such as size and demeanour.
You also classify objects according to their function. For example, there are commercial vehicles and recreational vehicles. There are trucks and passenger cars. You classify cars according to their make and model. You need to use object hierarchies and classifications to make sense of the world.
You use inheritance in OOP to classify the objects in your programs according to common characteristics and functions. This makes working with the objects easier and more intuitive. It also makes programming easier because it enables you to combine general characteristics into a parent object and inherit these characteristics in the child objects.
For example, you can define an employee object that defines all the general characteristics of employees in your company. You can then define a manager object that inherits the characteristics of the employee object but also adds characteristics unique to managers in your company. Because of inheritance, the manager object will automatically reflect any changes to the characteristics of the employee object.
Aggregation is when an object consists of a composite of other objects that work together. For example, your lawn mower object is a composite of the wheel objects, the engine object, the blade object, and so on. The engine object is a composite of many other objects. There are many examples of aggregation in the world around us.
The ability to use aggregation in OOP is a powerful feature that enables you to accurately model and implement business processes in your programs.