Table of content:
- What Is Object-Oriented Programming?
- Attributes and Methods
- Basic concepts related to objects
- Class
- Abstraction
- Inheritance
- Encapsulation
- Polymorphism
- Advantages of Object-Oriented Programming
- Disadvantages of Object-Oriented Programming
In programming, there are different approaches that can be taken in order to build a particular product or software. Among these, Object-Oriented Programming is a foundational programming approach that is used by many developers throughout the world. It is highly popular and has become a standard to build applications and to write high-quality code. There are multiple concepts and principles that make up Object-Oriented Programming. The explanation of these concepts along with their examples is covered here.
What is Data Handling? Understanding The Basics
What Is Object-Oriented Programming?
Object-oriented programming, which is commonly referred to as OOPs is a kind of a programming approach where a concept known as objects are made use of. Objects, in the context of programming, are a unique type of entity that are used to hold some data and code together. This is best understood by considering an analogy with real-world objects.
Let’s first try to understand what an object is. To keep things simple, think of any random object that you may encounter as a real-world entity. For instance, this can be the remote control of a television. Now, this object has primarily two kinds of properties. The first is its nature, and the second is its behavior. The nature of the remote consists of things like its color, number of buttons, shape, size and so on. On the other hand, its behavior consists of the things like turning on or off the TV, changing the channel or the volume, showing the menu and so on.
In the world of programming, objects can be understood as abstract entities that contain data, also known as attributes as well as methods or functions. With this knowledge, let’s understand the definition of OOP. Object-oriented programming is the approach of using objects and classes along with their related features to build a robust system. Object-oriented design refers to the use of objects and their interaction for a software-related product. Therefore, an object composition consists of two main things-its data and its methods. This is also known as object data and object methods.
Let’s say that there is a requirement to create a variable to hold of today’s date. One way of doing this would be to create a numeric variable and an alphanumeric variable such as a string to store the date and the day of the week. Another way would be to create a class that contains data types to store a number and a string. It can also contain methods that might be used to modify the date. This is how an object works. The class is used as a blueprint to create an object. Once the class is made, the programmer can use it to create as many objects as required and can call upon their methods under different conditions.
Attributes and Methods
The information that is saved is referred to as attributes. The class blueprint defines the attributes. Individual objects have data saved in the attributes field when they are instantiated or created.
Behaviors are represented through methods. Methods carry out operations, such as returning information about an object or updating its data. The code for the method is specified in the class specification.
As an example, let’s say that there is a class representing a car. This class acts as a template to create objects. Since it is a template, it should specify the attributes and methods that the objects will carry. This is shown in the picture below. The attributes can be things like the color of the car, the model and so on. The methods can be repaint the car, turn off the engine, turn on the wipers and so forth.
When an object is created using this class, it will have all these attributes and methods already contained within it. They will only differ from one object to another. There can be one car object representing a black Mercedes and another one representing a red Ferrari.
This is how objects make it simpler to perform operations that might normally appear slightly difficult or lengthy.
What Is Computer Programming? A Brief About Computer Programming And Related Terms
Basic concepts related to objects
Here are some basic definitions and basic principles related to the use of objects and OOP concepts:
Class
Taking the help of earlier example, if you want to create a car, you’ll need a blueprint. Similarly, to create an object, you need a blueprint and this comes in the form of a class. A class is essentially an entity that defines how an object should be. It contains the data and methods that will make up the object. There can different kinds of classes as well, such as a parent class and a child class, which helps with inheritance. Objects can also be termed as instances of classes.
The next 4 concepts of abstraction, inheritance, encapsulation and polymorphism are also known as the four pillars that make up the basis of object-oriented programming.
Abstraction
We are surrounded by extremely complex things, yet on the surface, they look quite simple to us. A laptop is made up of a lot of parts, all working together to form one coherent system. But we don’t look at the laptop as a thing that has a billion transistors in its CPU, or which has an HD screen, or which has a mechanical keyboard. We look at it as one single object, that is a laptop. The same is true in OOP. Abstraction is the process where a system only displays the most essential parts of a user and hides the remaining complex part behind it.
Inheritance
Inheritance is a concept that allows one class to inherit the properties of another, just like a child may inherit the properties of their parent. In terms of programming, there may be a child class that can inherit the properties and attributes of its parent class. The parent class is also known as the base class. The relation between them gives rise to inheritance, also known as “has-a” relationships since a parent “has a” child. This also helps in code reusability.
Encapsulation
Encapsulation is the idea of combining code and data together within a single unit. There is also a second purpose here. Encapsulation not only helps to keep code and data together but also protects it from unauthorized outside access.
The picture below describes what encapsulation looks like. The data variables and methods are enclosed within a class and can only be accessed by other members of the class. This helps in code access in important projects. There may be a public method or private variables, all of which are encapsulated within a class.
Polymorphism
At its basic level, polymorphism means having many forms. In the terms of programming, polymorphism is used as a concept that enables us to perform a single task in multiple ways. It can also denote that there may multiple things with the same name but performing different functions.
Let us say that you have a program into which you can enter the values of lengths of geometric shapes in order to find their area. Since the final output of the program will be the area of the shape, you can create multiple functions in the program all having the same name, that is “area”, but performing different functions such as one finds the area of a triangle, another one of a circle, and so on. There are two cases of polymorphism, the first is runtime polymorphism and the second is compile-time polymorphism.
Apart from the above four concepts, there are some additional ideas that define the relationships between objects which are as follows:
1. Association: The term ‘association’ refers to a relationship that exists between two distinct classes that are established through their Objects. One-to-one, one-to-many, many-to-one, and many-to-many associations are all possible. An Object communicates with another object in Object-Oriented programming to leverage the capabilities and services offered by that object. The two types of association are composition and aggregation.
2. Composition: A composition is a form of ‘belongs-to’ connection in which one item is logically connected to another. It’s also known as a “has-a” relationship. An example of this is a bedroom in an apartment. The bedroom is the child object and the apartment is the parent object. The apartment “has-a” bedroom and hence their relationship can be called composition. This is also known as a tightly coupled relationship, wherein if the parent object is deleted, then the child object cannot exist on its own. A bedroom can’t exist on its own if there is no apartment.
3. Aggregation: This is also another kind of a “has-a” connection known as an aggregation relationship. The sole distinction between Aggregation and Composition is that Aggregation does not entail ownership or closely connected items. An example of this is a car and a seat. The car here is a parent object and its seat is the child object. It’s visible that a car “has-a” seat, and hence their relationship is known as a composition relation.
All of the objects are self-contained and can exist even if the parent object is removed. A car seat can still exist on its own if there is no car.
Advantages of Object-Oriented Programming
An object-oriented programming language, such as Java or C++makes use of all these aforementioned programming paradigms. Object-oriented languages can make use of all the above-mentioned principles of abstraction, inheritance and all other features of OOPs, providing them with greater utility, power and the ability of code reuse. These are some of the common properties that all OOP languages follow are:
- An object can be understood as a user-defined variable. Usually, languages allow us the feature of creating variables that can store numbers, characters or strings, but what if a more complex data type is required? In such a case, the user can create a class that contains the required data variables and functions to be used. Once an object is created using this class, the user can make use of their own complex and customized data type.
- Object-oriented languages make the process of debugging easier through the concept of objects. If there are parts of the code that don’t work as desired, it’s much simpler to go through individual classes and their objects than having to scan the entire file for errors. The object-oriented programming technique of using encapsulation further enforces this idea.
- Allows the use of data hiding, through the use of encapsulation and abstraction. This allows developers to display a complex system as a simple and basic unit. Abstract classes also help in code reuse and polymorphism. Hence code duplication is avoided.
- Object-oriented programs provide flexibility through the help of polymorphism. Multiple functions with the same name can be used to perform different operations depending on the requirement. This makes the process of creating and remembering names slightly easier.
- Efficient use of code can be made by using parent and child classes. Inheritance makes it easier to reuse code and make a child object conveniently from a parent object. It is possible to build an inheritance hierarchy by using the multiple types of inheritance options that a language allows.
- It is also possible to create a collection of objects or an array of objects. This allows developers to create an entire set of objects that they might require depending on the application. This makes it easier to store a large number of objects together in a proper data structure.
- Object-oriented programming is all about breaking down a large problem into manageable bits. You create a class for each mini-problem that fulfils your requirements. Then, best of all, you can reuse those classes, making the next challenge much easier to tackle.
Disadvantages of Object-Oriented Programming
- Because OOP is not a universal language, we can’t use it everywhere. It is only used when absolutely necessary. It is not appropriate for all sorts of issues. It can be only used with languages that support it.
- The length of programs written in the OOP language is significantly greater than that of programs written in the procedural technique. As the program grows in size, it takes longer to run, resulting in slower program execution.
- Because utilizing OOP is a little challenging, programmers must have excellent design patterns and programming skills, as well as adequate planning.
- It takes some time to become acclimatized to OOPs. For some people, the thinking process involved with object-oriented programming is not natural.
Summing Up
OOP ideas in programming languages assist us in more efficiently structuring our software. The seven object-oriented principles we’ve looked at so far such as abstraction, encapsulation, polymorphism, inheritance, association, aggregation, and composition may help us reuse code, avoid security risks, and boost the speed of our software program. Object-oriented programming necessitates planning and thinking about the program’s structure before beginning to code. Examining how to decompose the requirements into basic, reusable classes that may be utilized to create object instances. Overall, using OOP provides for more reusable data structures and saves time in the long run.
You may also like to read: