what is OOPS?

Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around objects, which are data structures containing both data (attributes) and code (methods) that operate on that data.

what is OOPS?

INTRODUCTION OF OBJECT ORIENTED PROGRAMMING:

  • As the name suggests, Object-Oriented Programming or OOPs 
    refers to languages that use objects in programming. 
  • Object-oriented programming aims to implement real-world 
    entities like inheritance, hiding, polymorphism, etc in 
    programming. 
  • The main aim of OOP is to bind together the data and the functions 
    that operate on them so that no other part of the code can access 
    this data except that function.

OOPs Concepts:

  • Class
  • Objects
  • abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Dynamic Binding
  • Message Passing

CLASS


A class is a user-defined data type. It consists of data members and 
member functions, which can be accessed and used by creating an 
instance of that class. It represents the set of properties or methods 
that are common to all objects of one type. A class is like a 
blueprint for an object.  
For Example: Consider the Class of Cars. There may be many cars 
with different names and brands but all of them will share some 
common properties like all of them will have 4 wheels, Speed Limit, 
Mileage range, etc. So here, the Car is the class, and wheels, speed 
limits, and mileage are their properties.

Collection of objects is called class. It is a logical entity.
A class can also be defined as a blueprint from which you can 
create an individual object.

Class doesn't consume any space.

OBJECT


It is a basic unit of Object-Oriented Programming and represents 
the real-life entities. An Object is an instance of a Class. When a 
class is defined, no memory is allocated but when it is instantiated 
(i.e. an object is created) memory is allocated. An object has an 
identity, state, and behavior.


Each object contains data and code to manipulate the data. Objects 
can interact without having to know details of each other’s data or 
code, it is sufficient to know the type of message accepted and type 
of response returned by the objects. 


For example “Dog” is a real-life Object, which has some 
characteristics like color, Breed, Bark, Sleep, and Eats.

 


ABSTRACTION

Data abstraction is one of the most essential and important 
features of object-oriented programming. 
Data abstraction refers to providing only essential information 
about the data to the outside world, hiding the background details 
or implementation. 
Consider a real-life example of a man driving a car. The man only 
knows that pressing the accelerators will increase the speed of the 
car or applying brakes will stop the car, but he does not know 
about how on pressing the accelerator the speed is increasing, he 
does not know about the inner mechanism of the car or the 
implementation of the accelerator, brakes, etc in the car. This is 
what abstraction is
Hiding internal details and showing functionality is known as 
abstraction.

For example phone call, we don't know the internal processing.
In Java, we use abstract class and interface to achieve abstraction.

ENCAPSULATION

 
Binding (or wrapping) code and data together into a single 
unit are known as encapsulation. For example, a capsule, it 
is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is 
the fully encapsulated class because all the data members 
are private here

Encapsulation is defined as the wrapping up of 
data under a single unit. 
It is the mechanism that binds together code and 
the data it manipulates. 
In Encapsulation, the variables or data of a class 
are hidden from any other class and can be 
accessed only through any member function of 
their class in which they are declared. As in 
encapsulation, the data in a class is hidden from 
other classes, so it is also known as data-hiding.

INHERITANCE


Inheritance is an important pillar of OOP(Object-Oriented 
Programming). 
The capability of a class to derive properties and characteristics from 
another class is called Inheritance. 
When we write a class, we inherit properties from other classes. 
So when we create a class, we do not need to write all the properties and 
functions again and again, as these can be inherited from another class 
that possesses it. Inheritance allows the user to reuse the code whenever 
possible and reduce its redundancy.

When one object acquires all the properties and behaviors of a parent 
object, it is known as inheritance. 
It provides code reusability. 
It is used to achieve runtime polymorphism.

POLYMORPHISM


The word polymorphism means having many forms.
In simple words, we can define polymorphism as the ability of a message 
to be displayed in more than one form. 
For example, A person at the same time can have different 
characteristics. Like a man at the same time is a father, a husband, an 
employee. So the same person posses different behavior in different 
situations. This is called polymorphism.

If one task is performed in different ways, it is known as polymorphism.
For example: to convince the customer differently, to draw something, 
for example, shape, triangle, rectangle, etc.
In Java, we use method overloading and method overriding to achieve 
polymorphism.
Another example can be to speak something; for example, a cat speaks 
meow, dog barks woof, etc

DYNAMIC BINDING


In dynamic binding, the code to be executed in response to the function 
call is decided at runtime. 
Dynamic binding means that the code associated with a given procedure 
call is not known until the time of the call at run time. 
Dynamic Method Binding One of the main advantages of inheritance is 
that some derived class D has all the members of its base class B. Once D 
is not hiding any of the public members of B, then an object of D can 
represent B in any context where a B could be used. This feature is 
known as subtype polymorphism.

MESSAGE PASSING


It is a form of communication used in object-oriented programming as 
well as parallel programming.

Objects communicate with one another by sending and receiving 
information to each other. 
A message for an object is a request for execution of a procedure and 
therefore will invoke a function in the receiving object that generates the 
desired results. Message passing involves specifying the name of the 
object, the name of the function, and the information to be sent.

COUPLING

Coupling refers to the knowledge or information or dependency of 
another class. It arises when classes are aware of each other.

If a class has the details information of another class, there is strong 
coupling. 


In Java, we use private, protected, and public modifiers to display the 
visibility level of a class, method, and field.

ASSOCIATION


Association represents the relationship between the objects. Here, one 
object can be associated with one object or many objects. There can be 
four types of association between the objects:

  • One to One
  • One to Many
  • Many to One, and
  •  Many to Many
       
    For example, One country can have one prime minister (one to one), and 
    a prime minister can have many ministers (one to many). Also, many 
    MP's can have one prime minister (many to one), and many ministers 
    can have many departments (many to many).

AGGREGATION


Aggregation is a way to achieve Association. 

Aggregation represents the relationship where one object contains other 
objects as a part of its state.

It represents the weak relationship between objects. 

It is also termed as a has-a relationship in Java. 

Like, inheritance represents the is-a relationship. 

It is another way to reuse objects.

COMPOSITION


The composition is also a way to achieve Association. 
The composition represents the relationship where one object contains 
other objects as a part of its state. 
There is a strong relationship between the containing object and the 
dependent object. 
It is the state where containing objects do not have an independent 
existence. 
If you delete the parent object, all the child objects will be deleted 
automatically.

EXCEPTION HANDLING


Exceptions: Exceptions are runtime anomalies or unusual conditions 
that a program may encounter while executing .

Anomalies might include conditions such ass division by zero, accessing 
an array outside of its bounds or running out of memory or disk space. 
When a program encounters an exception condition, it must be 
identified and handled.

Exceptions provide a way to transfer control from one part of a program 
to another. C++ exception handling is built upon three keywords: try, 
catch, and throw

Types of exceptions:

There are two kinds of exceptions


 1.Synchronous exceptions
 2.Asynchronous exceptions


 1. Synchronous exceptions: Errors such as “Out-of-range index” and “over 
flow” are synchronous exceptions.


 2. Asynchronous exceptions: The errors that are generated by any event 
beyond the control of the program are called asynchronous exceptions.

EXCEPTION HANDLING MECHANISM:


An exception is said to be thrown at the place where some error or 
abnormal condition is detected. The throwing will cause the normal 
program flow to be aborted, in a raised exception.
An exception is thrown programmatic, the programmer specifies the 
conditions of a throw. 

In handled exceptions, execution of the program will resume at a 
designated block of code, called a catch block, which encloses the point 
of throwing in terms of program execution. The catch block can be, and 
usually is, located in a different function than the point of throwing.
C++ exception handling is built upon three keywords: try, catch, and 
throw.

Try is used to preface a block of statements which may generate 
exceptions. This block of statements is known as try block.

When an exception is detected it is thrown by using throw statement in 
the try block. Catch block catches the exception thrown by throw 
statement in the try block and handles it appropriately.

  

Procedural Programming vs Object-Oriented Programming:

Below are some of the differences between procedural and object-oriented programming: 

Procedural Oriented Programming Object-Oriented Programming

In procedural programming, the program is divided into small parts called functions.

In object-oriented programming, the program is divided into small parts called objects.
Procedural programming follows a top-down approach. Object-oriented programming follows a bottom-up approach.
There is no access specifier in procedural programming. Object-oriented programming has access specifiers like private, public, protected, etc.
Adding new data and functions is not easy. Adding new data and function is easy.
Procedural programming does not have any proper way of hiding data so it is less secure. Object-oriented programming provides data hiding so it is more secure.
In procedural programming, overloading is not possible. Overloading is possible in object-oriented programming.
In procedural programming, there is no concept of data hiding and inheritance. In object-oriented programming, the concept of data hiding and inheritance is used.
In procedural programming, the function is more important than the data. In object-oriented programming, data is more important than function.
Procedural programming is based on the unreal world. Object-oriented programming is based on the real world.
Procedural programming is used for designing medium-sized programs. Object-oriented programming is used for designing large and complex programs.
Procedural programming uses the concept of procedure abstraction. Object-oriented programming uses the concept of data abstraction.
Code reusability absent in procedural programming, Code reusability present in object-oriented programming.
Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc.

 Q. Who invented OOP?

  1. Andrea Ferro
  2. Adele Goldberg
  3. Alan Kay
  4. Dennis Ritchi


Alan Kay invented OOP, Andrea Ferro was a part of SmallTalk 
Development. Dennis invented C++ and Adele Goldberg was in 
team to develop SmallTalk but Alan actually had got rewarded for 
OOPs.

Q. Which is not a feature of OOP in general definitions?
 

  • Efficient Code
  • Code reusability
  • Modularity
  • Duplicate/Redundant data
     

Q. Which feature of OOP indicates code reusability?
 

  •  Abstraction
  • Polymorphism
  • Encapsulation
  • Inheritance
     
    Inheritance indicates the code reusability. Encapsulation and 
    abstraction are meant to hide/group data into one element. 
    Polymorphism is to indicate different tasks performed by a single 
    entity.

Q. What is encapsulation in OOP?

  • It is a way of combining various data members and member 
    functions that operate on those data members into a single unit.
  • It is a way of combining various data members and member 
    functions into a single unit which can operate on any data.
  • It is a way of combining various data members into a single unit.
  • It is a way of combining various member functions into a single 
    unit.


 Q. Which of the following is not true about polymorphism?
 

  • Helps in redefining the same functionality
  • Increases overhead of function definition always
  • It is feature of OOP
  • Ease in readability of program


Q. What is an abstraction in object-oriented programming?

  • Hiding the implementation and showing only the features
  • Hiding the important data
  • Hiding the implementation
  • Showing the important data


 
Q. Which feature of OOP reduces the use of nested classes?
 

  1. Inheritance
  2. Binding
  3. Abstraction
  4. Encapsulation
     

Q. Which of the following can be overloaded ?

  • Variable
  • Objects
  • Class
  • Function



 
 
Q. The major goal of object - oriented programming is 

  1. Speed 
  2. Reuse
  3. User - interface
  4. Top - down program development



 
 
Q. Which of the following most accurately describes "multiple 
inheritance"?
 

  • When two classes inherit from each other.
  • When a child class has two or more parent classes.
  • When a base child has two or more derived classes.
  • When a child class has both an "is a" and "has a" relationship 
    with its parent class.


 
 

Q. Assertion (A): C++ is an object-oriented programming 
language.
Reason (R): C++ supports class, inheritance, templates, and 
exception handling.
 

  1.  Both (A) and (R) are true and (R) is correct explanation of (A).
  2. Both (A) and (R) are true, but (R) is not the correct explanation 
    of (A).
  3. (A) is true, but (R) is false.
  4. (A) is false, but (R) is true.


 

 
Q. An object-oriented programming concept that refers to the 
ability of a variable, function, or object to take on multiple forms is :

  • Inheritance
  • Hierarchy
  • Polymorphism
  • State Transition
     

Q. If a class C is derived from class B, which is derived from class 
A, all through public inheritance, then a class C member function 
can access 

  • Only protected and public data of C and B
  • Only protected and public data of C
  • All data of C and private data of A and B
  • Public and protected data of A and B and all data of C 



 
1. public inheritance:
 In this, public members of the base class remain 
public in the derived class and protected 
members of base class remains protected in base 
class.
 

2.  private inheritance:
 in this, each protected and public member of 
base class remains private in the derived class.
 

3.  protected inheritance:
 In this, each private and public member of base 
class remains protected in the derived class.