Object-Oriented-Programming
Object-Oriented-Programming
Some of e.g. of OOP C++, Java, JavaScript, PHP, C#, VB.Net, etc.
Features/Characteristics of OOP
a) Emphasis is on data rather than procedure.
b) Programs are divided into objects.
c) Data structures are designed such that they characterize the objects.
d) Functions that separate on the data of an object are tied together in the data structure.
e) Data is hidden and cannot be accessed by external functions.
f) Objects may communicate with each other through functions.
g) New data and functions can be easily added whenever necessary.
h) Bottom-up approach is followed in program design.
Advantages of OOP:
a) We can eliminate redundant codes by using inheritance feature of OOP.
b) It is very easy for managing complex and large size problems.
c) The most important is the reusability of codes by using the features inheritance.
d) It takes very less time for the development and maintaining the software.
e) It is efficient for testing and implementation of the system.
OOP has several disadvantages which made it unpopular in the early years.
a) Size: Object oriented programs are much larger than other programs. In the early days of
computing, space on hard drives, floppy drives and in memory was at a premium. Today
we do not have these restrictions.
b) Effort: Object oriented programs require a lot of work to create. Specifically, a great deal
of planning goes into an object oriented program well before a single piece of code is
ever written. Initially, this early effort was felt by many to be a waste of time. In addition,
because the programs were larger coders spent more time actually writing the program.
c) Speed: Object oriented programs are slower than other programs, partially because of
their size. Other aspects of Object Oriented Programs also demand more system
resources.
Features of OOP
Object: An object is any entity, thing or organization that exits in real world. It consists of two
fundamentals characteristics: its attributes and behaviors. For example: a dog is an object having
attributes such as color, weight, age, etc. and behaviors such as barking, wagging tail, etc. In
OOP, attributes are represented by data and behaviors are represented by functions.
Class: A class is the collection of similar objects. It is defined as the template or prototype to
define the common attributes and behavior for all the objects of the class. The entire set of data
and code of an object can be made a user-defined data type with the help of a class. In fact
objects are variables of type class. Once, a class has been defined, we can create any number of
objects associated with that class.
Real life Example of Class and object
Consider an ATM. An ATM is a class. It’s machine which is pretty much useless until you insert
your debit card. After you insert your debit card, the machine has information about you and
your bank account and the balance in it, so at this point it is an object. You have used a class and
created an object, now you can perform operations on it like withdrawal of money or checking
you balance or getting statement of your account, these operations will be methods belonging to
that class (ATM) but you cannot use them until you create an object out of it.
And when you did perform whatever operation you wanted to perform and clicked exit/cancel
and removed your card, you just destroyed the object. Now it is not an object, it has methods and
all (the functions an ATM can perform) but you cannot use them until you insert your card again
and create an object.
Classes and objects are the two main aspects of object-oriented programming.
Look at the following illustration to see the difference between class and objects:
Class objects
Fruit Apple
Banana
Mango
Another example:
Class objects
Car Volvo
Audi
Toyota
When the individual objects are created, they inherit all the variables and functions from the
class.
Data abstraction: It is a collection of data and methods. In OOP, data abstraction defines the
conceptual boundaries of an object. These boundaries distinguish an object from another object.
So, abstraction is the act of representing essential features without including the background
details. It focuses the outside view of an object, separating its essential behavior from its
implementation.
Data encapsulation is the mechanism that binds code and data it manipulates and keeps both safe
from outside interference and misuse. The data are visible only to that particular function, called
data hiding.
Polymorphism : The word "Polymorphism" is derived from Greek word "Poly-morphos" which
means many forms. The different ways of using same function or operator depending on what
they are operating on is called polymorphism. In OOP, polymorphism refers to a programming
language's ability to process objects differently depending on their data type or class.
For eg an operator symbol '+' which is used for arithmetic operation among two numbers,
but by overloading we use it for complex object like time that has hr, minutes and second as its
attributes. By overloading, same operator '+' we can also perform that string concatenation.
Inheritance : Inheritance is the process by which objects of one class acquire the properties of
objects of another class. For e.g, all vehicles in a class may share similar properties of having
wheels and a motor.
Inheritance is a way to form new classes(instance of which are called objects)
Types of Inheritance :
a) single inheritance : In this inheritance, there is a derived class with only one base class.
i.e if a child class is built from only one parent class.
b) Multi Inheritance : In this inheritance, the derived class has several base classes. If a child
class is built from two or more than two parents classes.
c) Hierarchical Inheritance : In this inheritance, the behavior of one class may be derived by
more than one class.
d) Multiple Inheritance : In this inheritance, the mechanism of deriving a class from another
derived class.
Inheritance and polymorphism important feature in OOP
The process of creating a new class form an existing class in which objects of the new class
inherit the attributes and behaviors of the existing class is known as inheritance. The newly
created class is called derived class or child class and the class from which new class is derived
is called base class or parent class. It permits the expansion and reuse of existing code without
rewriting it hence, the concept of inheritance supports the concept of reusability.
The meaning of polymorphism is having many forms. It is an important feature of OOP which
refers to the ability of an object to take on different forms depending upon situations. It
simplifies coding and reduces the rework involved in modifying and developing an application.
What is OOP?
Procedural programming is about writing procedures or functions that perform operations on the
data, while object-oriented programming is about creating objects that contain both data and
functions.
What is OOP?
1. Polymorphism
2. Inheritance
3. Encapsulation
4. Abstraction
1. Polymorphism
Example 1: We can take a boy as a real-world example. This boy can be a student, a player, and a
writer. So that this boy can exist in different ways in different situations.
Example 2: Sound of animals. People have the same sound but different animals make different
sounds. The following diagram shows few different sounds make by animals.
This what we called as polymorphism.
2. Inheritance
Inheritance means it allows classes to inherit common properties from the parent class.
Example 1: let's assume that there is a class as Vehicle. All vehicles are not the same. We can
inherit common properties like color, size, type from the parent vehicle class and create classes
like Car, Bus, Bike.
Example 2: let's take another parent class as Animals. Here also we can inherit common
properties like name, sound, color, breed from Animal class and create classes like Dog, Cat,
3. Encapsulation
Encapsulation means it binds data and code together into one unit.
Example1; the most commonly used example is the medical capsule. This capsule mixes few
of several departments like the production department, purchase department, sales department,
and Accounts department. It combines all these departments together and had formed the
organization.
4. Abstraction
In abstraction, it displays only the important information by hiding the implementation part.
Example 1: Let's take the ATM machine. In an ATM machine, we can perform functions like
withdraw cash, deposit cash, check balance, print bills, and so on. Even though it performs a lot of
actions it doesn’t show us the process. It has hidden its process by showing only the main things
can perform so many actions like making a call, sending messages, take pictures, download
software and etc. We perform a lot of things but here also we don't know the inside process of