Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

OOPs – Object-Oriented Programming is a programming model or paradigm which revolves around

the concept of “OBJECTS”. Objects can be considered as real-world instances of entities like class,
that contain some characteristics and behaviours specified in the class template.
In simple language, a class can be considered as the blueprint or template, based on which objects can
be created. So, the Objects are considered the instance of a class, and are therefore sometimes called
“instances”. The term “characteristics” refers to the “what” about the Object, and the term
“behaviour” refers to the “how” about the Object.
Need for OOPs -
There are many reasons why OOPs is mostly preferred, but the most important among them are: 
o OOPs helps users to understand the software easily, although they don’t know the actual
implementation.
o With OOPs, the readability, understandability, and maintainability of the code increase multi-
fold.
o Even very big software can be easily written and managed easily using OOPs.

OPPs is used in:


o Python
o Java
o Go
o Dart
o C++
o C#
o Ruby

Main pillars/features of OOPs –


OOPs has 4 main pillars:
1. Inheritance – The term means “receiving some quality or behaviour from a parent to an
offspring.” In object-oriented programming, inheritance is the mechanism by which an object
or class (referred to as a child) is created using the definition of another object or class
(referred to as a parent). Inheritance not only helps to keep the implementation simpler but
also helps to facilitate code
For example - Let's take three different vehicles - a car, truck, or bus. These three are entirely
different from one another with their own specific characteristics and behaviour. But. in all
three, you will find some common elements, like steering wheel, accelerator, clutch, brakes,
etc. Though these elements are used in different vehicles, still they have their own features
which are common among all vehicles. This is achieved with inheritance. The car, the truck,
and the bus have all inherited the features like steering wheel, accelerator, clutch, brakes, etc,
and used them as their own. Due to this, they did not have to create these components from
scratch, thereby facilitating code reuse.

2. Encapsulation – One can visualize encapsulation as the method of putting everything that
is required to do the job, inside a capsule and presenting that capsule to the user. What it
means is that by Encapsulation, all the necessary data and methods are bind together and all
the unnecessary details are hidden to the normal user. So, Encapsulation is the process of
binding data members and methods of a program together to do a specific job, without
revealing unnecessary details.
3. Polymorphism – It refers to the process by which some code, data, method, or object
behaves differently under different circumstances or contexts.
Compile-time polymorphism and Run time polymorphism are the two types of
polymorphisms in OOPs languages.

For Example – A person plays different roles at different situations like employee, father,
husband, son, passenger, patient, etc.

4. Abstraction – If you are a user and you have a problem statement, you don't want to know
how the components of the software work, or how it's made. You only want to know how the
software solves your problem. Abstraction is the method of hiding unnecessary details from
the necessary ones. It is one of the main features of OOPs.
For example, consider a car. You only need to know how to run a car, and not how the wires
are connected inside it. This is obtained using Abstraction.
How does C++ support Polymorphism –

C++ is an OOPs language and supports Polymorphism as well:

 Compile Time Polymorphism : C++ supports compile-time polymorphism with the help of
features like templates, function overloading, and default arguments.
 Runtime Polymorphism:  C++ supports Runtime polymorphism with the help of features
like virtual functions. Virtual functions take the shape of the functions based on the type of
object in reference and are resolved at runtime.

Polymorphism-

Object-Oriented Programming vs Structural Programming –

It follows a bottom-up approach. It follows a top-down approach.


It provides data hiding. Data hiding not allowed.
It is based on objects rather than functions and It Provides a logical structure to a program in
procedures. which the program is divide into functions.
More abstraction more flexibility. Less abstraction less flexibility.
It focuses on data. It focuses on the process or logic structure.

Pure Object-Oriented Programming Language –


The programming language is called pure object-oriented language that treats everything inside the
program as an object. The primitive types are not supported by the pure OOPs language. There are
some other features that must satisfy by a pure object-oriented language:
o Encapsulation

o Inheritance

o Polymorphism

o Abstraction

o All predefined types are objects

o All user-defined types are objects


o All operations performed on objects must be only through methods exposed to the objects.

Java is not a pure object-oriented programming language because pre-defined data types in Java
are not treated as objects. Hence, it is not an object-oriented language.

Class and Object –

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 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.

An object is a real-world entity that has attributes, behaviour, 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 behaviours

Is it always necessary to create objects from class?

NO, an object is necessary to be created if the base class has non-static methods. But if the class has
static methods, then objects don’t need to be created. You can call the class method directly in this
case, using the class name.

How much memory does a class occupy?


Classes do not consume any memory. They are just a blueprint based on which objects are created.
Now when objects are created, they actually initialize the class members and methods and therefore
consume memory.

Function Overloading – It is static/compile time polymorphism. In C++, it provides same function


name with different parameters. It provides multiple definitions of the function by changing signature
i.e., changing number of parameters, change datatype of parameters, return type doesn’t play any role.

For example – In below, code volume when called in main function executes volume function in
accordance with parameters inserted.

Function Overriding – It is dynamic/run-time polymorphism. In C++, it provides same function


name with same parameters. Inheritance should be there if using overriding. It cannot be done with in
a class.

For this we require derived and base class so as to remove confusion.

For example – Here, we create different classes and call by b.show() and d.show().

You might also like