What is Object-Oriented Programming?
Object-oriented programming has a sweeping impact because it appeals at multiple levels and promises faster and cheaper development and maintenance. It follows a bottom-up approach to develop applications.
In this section, we will discuss in-depth what is object-oriented programming?
The word object-oriented is the combination of two words i.e. object and oriented. The dictionary meaning of the object is an article or entity that exists in the real world. The meaning of oriented is interested in a particular kind of thing or entity. In layman's terms, it is a programming pattern that rounds around an object or entity are called object-oriented programming.
The technical definition of object-oriented programming is as follows:
The object-oriented programming is basically a computer programming design philosophy or methodology that organizes/ models software design around data, or objects rather than functions and logic.
An object is referred to as a data field that has unique attributes and behavior. Everything in OOP is grouped as self-sustainable objects.
It is the most popular programming model among developers. It is well suited for programs that are large, complex, and actively updated or maintained. It simplifies software development and maintenance by providing major concepts such as abstraction, inheritance, polymorphism, and encapsulation. These core concepts support OOP.
A real-world example of OOP is the automobile. It more completely illustrates the power of object-oriented design.
Points to Remember
Pillars of OOPs
The major concepts that we have discussed above are known as pillars of OOPs. There are four pillars on which OOP rests.
Let's discuss each in detail.
The concept allows us to hide the implementation from the user but shows only essential information to the user. Using the concept developer can easily make changes and added over time.
There are the following advantages of abstraction:
Encapsulation is a mechanism that allows us to bind data and functions of a class into an entity. It protects data and functions from outside interference and misuse. Therefore, it also provides security. A class is the best example of encapsulation.
The concept allows us to inherit or acquire the properties of an existing class (parent class) into a newly created class (child class). It is known as inheritance. It provides code reusability.
The word polymorphism is derived from the two words i.e. ploy and morphs. Poly means many and morphs means forms. It allows us to create methods with the same name but different method signatures. It allows the developer to create clean, sensible, readable, and resilient code.
The above figure best describes the concepts of polymorphism. A person plays an employee role in the office, father and husband role in the home.
The OOPs concepts include the following:
An object is a real-world entity that has attributes, behavior, and properties. It is referred to as an instance of the class. It contains member functions, variables that we have defined in the class. It occupies space in the memory. Different objects have different states or attributes, and behaviors.
A class is a blueprint or template of an object. It is a user-defined data type. Inside a class, we define variables, constants, member functions, and other functionality. it binds data and functions together in a single unit. It does not consume memory at run time. Note that classes are not considered as a data structure. It is a logical entity. It is the best example of data binding. Note that a class can exist without an object but vice-versa is not possible.
The following figure best illustrates the class and object in OOP.
Apart from these core concepts, there are some other object-oriented concepts used in OOP.
In programming, separation of concerns is known as coupling. It means that an object cannot directly change or modify the state or behavior of other objects. It defines how closely two objects are connected together. There are two types of coupling, loose coupling, and tight coupling.
Objects that are independent of one another and do not directly modify the state of other objects is called loosely coupled. Loose coupling makes the code more flexible, changeable, and easier to work with.
Objects that depend on other objects and can modify the states of other objects are called tightly coupled. It creates conditions where modifying the code of one object also requires changing the code of other objects. The reuse of code is difficult in tight coupling because we cannot separate the code.
Since using loose coupling is always a good habit.
In OOP, cohesion refers to the degree to which the elements inside a module belong together. It measures the strength of the relationship between the module and data. In short, cohesion represents the clarity of the responsibilities of a module. It is often contrasted with coupling.
It focuses on a how single module or class is intended. Higher the cohesiveness of the module or class, better is the object-oriented design.
There are two types of cohesion, i.e. High and Low.
High cohesion often associates with loose coupling and vice versa.
Composition is one of the vital concepts in OOP. It describes a class that references one or more objects of other classes in instance variables. It allows us to model a has-a association between objects. We can find such relationships in the real world. For example, a car has an engine. the following figure depicts the same
The main benefits of composition are:
The association defines the relationship between the objects. Note that an object can be associated with one or more than one object. The relationship can be unidirectional or bidirectional. There are the following types of association.
It is an advanced form of association in which each object has its own Lifecycle but there exists ownership as well. In other words, a relationship where a child can exist independently of the parent. It is also termed as has-a relationship in Java. Like, inheritance represents the is-a relationship. It is another way to reuse objects.
Why should we use OOP?
Object-oriented programming is an evolutionary development in software engineering. Using OOP in software development is a good habit because it accomplishes the three major software engineering goals, as we have shown in the following figure.
Where it is used?
OOP is often the best use when we are dealing with manufacturing and designing applications. It provides modularity in programming. It allows us to break down the software into chunks of small problems that we then can solve one object at a time.
It should be used where the reusability of code and maintenance is a major concern. Because it makes development easy and we can easily append code without affecting other code blocks. It should be used where complex programming is a challenge.
Benefits of OOP
Limitations of OOP
List of Object-Oriented Programming Languages
There are various object-oriented programming languages are present. But we have enlisted some popular and widely used OOP languages.
Applications of OOPs