Object-Oriented Programming (1)
Object-Oriented Programming (1)
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.
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
○ Everything is an object
○ Developer manipulates objects that uses message passing.
○ Every object is an instance of a class.
○ The class contains the attribute and behavior associated with an object.
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.
○ Abstraction
○ Encapsulation
○ Inheritance
○ Polymorphism
Abstraction
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:
○ It reduces complexity.
○ It avoids delicacy.
○ Eases the burden of maintenance
○ Increase security and confidentially.
Encapsulation
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.
Inheritance
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.
Polymorphism
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.
OOPs Concepts
The OOPs concepts include the following:
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
Object
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.
Class
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.
Coupling
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.
Advertisement
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.
Cohesion
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.
High cohesion often associates with loose coupling and vice versa.
Composition
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
Association
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.
○ One to One
○ One to Many
○ Many to One, and
○ Many to Many
Aggregation
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.
Benefits of OOP
○ Modular, scalable, extensible, reusable, and maintainable.
○ It models the complex problem in a simple structure.
○ Object can be used across the program.
○ Code can be reused.
○ We can easily modify, append code without affecting the other code blocs.
○ Provides security through encapsulation and data hiding features.
○ Beneficial to collaborative development in which a large project is divided
into groups.
○ Debugging is easy.
Limitations of OOP
○ Requires intensive testing processes.
○ Solving problems takes more time as compared to Procedure Oriented
Programming.
○ The size of the programs created using this approach may become larger
than the programs written using the procedure-oriented programming
approach.
○ Software developed using this approach requires a substantial amount of
pre-work and planning.
○ OOP code is difficult to understand if you do not have the corresponding
class documentation.
○ In certain scenarios, these programs can consume a large amount of memory.
○ Not suitable for small problems.
○ Takes more time to solve problems.
There are various object-oriented programming languages are present. But we have
enlisted some popular and widely used OOP languages.
According to the TIOBE index, the top twenty OOP languages are Java, C++, C#,
Python, R, PHP, Visual Basic.NET, JavaScript, Ruby, Perl, Object Pascal,
Objective-C, Dart, Swift, Scala, Kotlin, Common Lisp, MATLAB, and Smalltalk.
Applications of OOPs
○ Computer graphics applications
○ Object-oriented database
○ User-interface design such as windows
○ Real-time systems
○ Simulation and modeling
○ Client-Server System
○ Artificial Intelligence System
○ CAD/CAM Software
○ Office automation system