There are many methodologies used in programming, there are different
paradigms of programming languages.
One such is procedural programming which is a simple set of
computational tasks that are carried out during the execution. for
example C language.
This method is great if you want a simple code that has a simple
implementation. These languages are great for beginners as they need
less amount of data & the processes aren't difficult to
comprehend.
But if you wish to create a client-focused, efficient coded
software, the object-oriented method is the way to go.
Although it takes more time to work on object-oriented code, it is
worth the effort as the efficiency, flexibility, and scalability are
very high.
Object-oriented coding also removes the repetition of code as
procedural programming had repeated code in similar
functions.
To know more about procedural programming and the differences between
procedural and object-oriented programming click here.
So we have summarized why object-oriented programming is to be
used, thus we shall now learn more about object-oriented
programming.
This post is the third entry in the series of posts about
object-oriented programming.
If this is your first post on object-oriented programming here at
college integral, I suggest to the reader that if you do not have prior knowledge
or know-how of this concept or if you are starting, or if you wish
to check the previous posts about this topic click below.
In object-oriented programming, we will learn about objects,
classes, and the pillars of object-oriented programming methodology
like
Abstraction
Encapsulation
Inheritance
Polymorphism
Polymorphism
Objects & Classes
"An object is the embodiment of the concept that is class."
Objects were introduced as the instances of classes in languages as
a concept to deal with real-world problems.
These objects are synonymous with a real-world or abstract entity, that
is
“Object” can be a person, a company, a set of things, or
anything abstract or real."
All objects have three distinctions, the object's identity, its
behavioral aspects, and information about the said object.
This information we speak about this object can be considered as the
attributes or properties of the object. These attributes are what we call data or arguments passed to the
parameters of the function defining or handling the data of the said
objects.
Any real-world object like a chair or a car can be considered an
object.
For example, in the case of a person, let's say named "Alex", the
likes/dislikes, features like height, weight, etc. are all
attributes of Alex.
The person's name Alex is the identity here.
His behaviors are the functions in this code, some functions might
be like eating, sleeping, drinking, running, jogging, and
laughing.
These functions have parameters, these parameters and other details about
the object are considered data about the object.
"A class is a blueprint or a template for the attributes &
properties to be tied down to in an encoded form."
A class defines the object’s properties as its attributes and
corresponding arguments are provided to the parameters stated inside
the class methods (functions inside the class).
For example, All the functions like eating, and sleeping that were discussed
Alex will come under the person's class.
This class person will have these functions like eating and
sleeping and will have instance variables with default values, which
could be updated using constructors (will discuss this in a different
post).
A class has the following
1. instance variables
2. member functions
3. constructors
4. nested class & interfaces
5. fields & blocks
Whenever an object is created of a class, it is called the
instantiation of an object.
This object is made as an embodiment of a class. The values (arguments)
passes from the object to the class whether through a constructor or a
member function are stored into variables created inside the
class.
These variables that are created inside a class are the instance
variables.
These can have a default value as well in case no value is
passed.
These classes have fields and blocks as part of the inherent structure
of the class.
These fields are of different types but can be broadly classified as
primitive or reference types generally in almost all cases.
These member methods are functions that are based on getting the desired
result from the data of an object.
Abstraction
"The process of "abstraction" in object-oriented programming was
introduced primarily to ensure confidentiality of the
implementation details of the class and its functions.
This solution needed to solve this issue timely without the user
getting concerned over the confidentiality of underline working of
the said program."
This came in the form of an interface that was akin to a "black
box" meaning only the function, its parameters, and what the said
function does was told to the end-user, this came into realization
by the use of abstraction in the code of the program.
Understanding the distinction needed between the interface of the said function or
class and its internal working or implementation. resulted in the
program's inner working being hidden from unauthorized classes.
All of this resulted in a decrease in complexity, an increase in
security, and thereby decreasing maintenance issues.
Let's take the same example of Alex, Alex was asked to complete a
task, now is the result expected from Alex, is it the result or the
whole process he used to get to the result?
Yes, only the result mattered in this scenario and Alex might not be
comfortable with providing his process of doing the work as it can be
considered a trade secret or confidential information so is the case
of abstraction in coding.
Encapsulation
"Encapsulation is the binding of code and data together
into a single unit."
But what does it mean?
It means that the data, the fields, the member functions, instance
variables, and all the other parts of the class are capsuled up and
sealed, meaning that this whole set of code is a single unit.
The data stored in the class cannot be accessed directly but
through the member functions.
as access to components within the class is restricted.
For example, Alex works in a company, the company has a class
personal_data that stores personal, sensitive, and confidential data
for filling out necessary details in forms, projects, and identity
checks.
This kind of scenario presents us with the need of encapsulating the
data that is wrapping data into a unit and just cross-checking details
by comparing them using a computer solves the issue of confidentiality
of sensitive data.
This results in necessary details filled, for example by just calling
a function that compares the sensitive data input with the already
existing data and provides only the result of comparison between the
two sets of data and not the details, solves the issue and proves the
need of encapsulation.
Inheritance
Inheritance is the "acquiring" of properties from one
class to another class.
When a class inherits from another class, it means that we do not
have to write all the functions and details again, this reduces
redundancy and provides reusability to the code.
Inheritance can be of multiple types but the core concept remains the
same, let's learn about inheritance, and afterward let's take a look
at the different types of inheritance.
In inheritance, there are two different roles taken by the different
blocks of code that are the "parent and the child "or "the base class
and the derived class "
In the case of a class that is inheriting the properties, the class
will be called the child class as a child inherits from the parent.
Types of inheritance:
1. Single inheritance: a class inherits from a class
2. Multiple Inheritance: one child class inherits from many
parent classes
3. Multi-level Inheritance: the child class inherits from other
child classes which they themselves have inherited.
4. Hierarchical Inheritance: A parent class
imparting to more than one child class
5. Hybrid Inheritance: A combination of the above inheritance
We shall learn more about the different kinds of inheritance
later.
Inheritance can be seen in coding in the form of function overriding
This concept generally means that a function that exists in the
parent class is written inside the child class the function
written in the parent class with the same name is overridden,
meaning that the function written inside the derived class will take
precedence over the original function inside the parent class provided
the number and type of arguments given are the same.
For example, let's take a class animal and have a derived class
land_animal that inherits the animal class and its functions. Let's
take a function of the animal class like eating (every animal has to
eat) and write the function inside the land_animal class as every land
animal also eats.
But the difference arises in the fact that an animal has a larger and
more general concept of food as it is a very general class whereas the
land_animal class is more distinct and precise in its reach and
therefore requires a change in the internal implementation of the
function without changing the name.
Polymorphism
Polymorphism means many forms, derived from the word poly meaning
many and morph meaning form.
For example, Alex can have many different forms/roles in life
meaning he can be a father, a husband, a friend, a mentor, an
employee, an employer, and so on so forth.
This requires different parameters, meaning that a different paradigm
is set for each of the different roles that Alex adopts in life,
requiring different sets of inputs and results in different sets of
outputs.
There are many ways polymorphism can manifest in a language that is
object-oriented like java or python.
Mainly this feature can be understood by function overloading and
operator overloading.
I shall briefly go through their meaning, and in detail, we shall
learn later on.
Function overloading
This can be understood as a way to solve a problem with different input
values for the same kind of coding logic inside a function, so it is
"overloaded" to cover the full scope of the problem.
This can be understood as creating different methods with the same name
but different internal implementations.We have learned a lot about Object-oriented programming as a
general concept in this post, for further understanding and learning
topics like coupling and cohesion concerning object-oriented
programming will require you to understand this concept not from a
general view but through the lens of a language like java and
python.