Introduction to OOP
Object-Oriented Programming (or OOP) is a paradigm of programming in which programs are written and structured around objects rather than functions or logic. Here, objects are defined as data fields which have unique attributes and behavior. They contain data in the form of attributes and procedures in the form of methods. Object procedures can access and modify the data present in an object.
There are many OOP languages, with the most popular ones being class-based, where objects will be an instance of a class. A class is a container for data and procedures, also known as data members and member functions. Let us consider an example of an object as a car. A car has attributes like color, brand name, fuel capacity, etc. and it has methods to represent the behavior of a car like a start, accelerate, break, etc. A class is a blueprint of attributes and methods and does not occupy space, until and unless an object for that class is made.
Example:
class car
{
char name[20]; // name and colour are attributes
char colour[20];
public void start(){} //start is a method
};
void main()
{
car c1; //c1 is an object
}
Object-oriented programming targets to implement in programming, real-world entities like inheritance, polymorphism, encapsulation, etc. which we will see in detail. The main objective of OOP is to collectively bind data and the functions that operate on them, such that this data is accessible only by that function.
Principles of OOP
The four main principles of object-oriented programming are encapsulation, abstraction, inheritance, and polymorphism.
1. Encapsulation
Binding of data and methods into a single unit is called encapsulation. Encapsulation is accomplished when each object inside the class keeps its state private. The data inside this unit is not accessible by outside objects and only those functions inside this unit are able to access it. Thus, the object manages its state with the help of its methods, and to communicate with this object, we will require the help of the public methods of this class.
4.6 (3,144 ratings)
View Course
2. Abstraction
Abstraction is an extension of encapsulation. It means providing only the necessary information to the outside world while hiding the internal details of implementation. It reveals only the appropriate operations for other objects. The advantage of this is that we can change the implementation without affecting the class, as the method interface remains the same.
Let us take the example of a calculator, which takes the input from us, and on the press of a button, gives us the desired output, while sparing us the internal details of how it has arrived at that answer.
3. Inheritance
Often, objects are similar in functionality, sharing part of the logic but differing in the rest. So how do we reuse the common logic and separate the different logic? This can be achieved by inheritance. In inheritance, we create a new class called as child class which is derived from the existing class called the parent class, thus forming a hier0archy of classes. The child class reuses the data fields and methods that it requires from the parent class, and implements its unique functionality on its own.
For example, a vehicle can be a parent class, from which we can derive child classes like Bike and Car. They share the common properties of being able to run on fuel and carry passengers but differ in the number of passengers they can carry and more such properties.
4. Polymorphism
Polymorphism is the ability to take more than one form. Suppose we have a parent class and a few of its child classes. Now we want to use attributes from both the parent and the child classes, so how will it be achieved? This can be done using Polymorphism. In Polymorphism, abstract entities are executed in multiple ways. It gives a way to consume a class exactly like the parent class, such that there is no confusion with mixing the type of classes, and each child class continues to keep its methods the way it was. This can be done by reusing a parent interface so that the child class can implement these methods in their own version.
Advantages & Disadvantages of Object-Oriented Programming
Below are the advantages and disadvantages
Advantages
Below are the advantages:
- A real-world idea can be demonstrated, as everything in OOP is treated as an object.
- As we use the concept of encapsulation, programs are easier to test and maintain.
- Faster development of code is done, as we develop classes parallel instead of sequentially.
- OOP provides greater security due to data abstraction. The outside world cannot access the hidden data.
- Reusability can be achieved by using classes that have been already written.
Disadvantages
Below are the disadvantages:
- Designing a program with OOP concept can be tricky.
- A programmer needs to plan beforehand for developing a program in OOP.
- The size of programs developed with OOP is bigger than those developed with a procedural approach.
- Since OOP programs are larger in size, the execution time for these programs is also more.
How can Knowledge of OOP help in Career Growth?
Many of the major trending languages these days like Java and Ruby, use Object-oriented programming concepts. OOP languages help in writing software for applications such as mobile, web and gaming applications. There are high earnings in these fields, like the best job opportunities for programmers to lie in these fields. It is easy to move into various technologies and languages with the basics of OOP, and thus this widens our career prospects. One drawback in this happens to be expertise. Usually, companies look for practical experience in OOP languages and concepts, so it is recommended to practice as we go along the learning process.
Conclusion
Object-oriented programming simplifies the programming process for us. It has many values like reusability, efficiency, and maintenance of code. While it may initially be hard to understand OOPs concepts, I assure you the fruit will be worth the effort. Hope this article helped in simplifying those concepts for you!
Recommended Article
This has been a guide to What is OOP? Here we discussed the Concepts, and principles with the advantages and disadvantages. You can also go through our other suggested articles to learn more –