The object-Oriented Paradigm is a style of programming in which everything is considered to be an object. An object here is an instance of a class that holds data and methods to mutate/modify the data held by the object and hence, the object's state.
Before we begin to take a deep look, let us understand some terms related to this paradigm.
Classes and Objects
You can think of a class as a general view of a group of items.
An item belonging to a group is considered an object. An object is an instance of a class i.e a specific implementation.
Each group of items has something peculiar about them.
For example, consider Fruits. We know every fruit will have a taste, color, shape, etc. We then have pineapples, watermelon which is all specific examples of fruits. You can think of Fruits as being a general description/blueprint upon which every other fruit builds. Watermelon and pineapples could also be seen as objects of the class Fruit.
Every object has a property and attribute. These properties and attributes can be thought of as key-value pairs holding data about an object.
Methods
Methods are functions that are attached to a class. These sorts of functions can only be called on instances of that class except for static methods which can be called from the class itself.
This paradigm encourages reusability and helps us to group items under a specific type.
In this article, we are going to look at some important concepts in the Object-Oriented paradigm namely;
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Data Abstraction
Abstraction is a way of preventing the internal data held by an object from being accessed by some external agent.
This means that only methods of that class may access the data held by an object.
In languages like Java, this is typically done by adding access specifiers like private
, public
, or even protected
.
Data Abstraction here is a design pattern.
It is worth noting that even methods can be abstracted in an object preventing direct access.
Encapsulation
Encapsulation is sometimes confused with Data Abstraction mainly because they are centered around hiding stuff from a class. There is some sort of overlap here.
Unlike abstraction, encapsulation is concerned with unifying together data, and the methods that act on that data under a single unit. This is usually to hide the way data is structured in the class.
This means that the internal structure of data as described by the class might change but the user of the class need not be concerned by those changes as long as the method exposed by that class does the same operation.
Encapsulation is applied to restrict the direct access to object data.
Inheritance
This involves a class inheriting from another class.
Let's consider the Fruit class we described above.
We might have a higher class named LivingThing
from which our Fruit will extend.
A living thing can grow, eat and eventually die. Our fruit class, inheriting from the LivingThing
class
will therefore also be able to grow & eat.
In this context, we say LivingThing
is a super class or parent class of Fruit
while Fruit
in itself is a child class of LivingThing
.
Polymorphism
Polymorphism refers to providing a single generic view for objects of different types. This generic view is typically referred to as an interface.
Looking at our Fruit
example again, we know mangoes and watermelons both grow differently - but one thing is that they will both have a grow()
method attached to them.
This mechanism applies even during inheritance.
This means that this grow()
method may have been inherited from the LivingThing
class even though it has different implementations.
You have reached the end. If you want to learn about other design patterns, then consider this article.