OOP Interview Question
OOP Interview Question
There are many reasons why OOPs is mostly preferred, but the most important among
them are:
OOPs helps users to understand the software easily, although they don’t know the actual
implementation.
With OOPs, the readability, understandability, and maintainability of the code increase
multifold.
Even very big software can be easily written and managed easily using OOPs.
The programming languages that use and follow the Object-Oriented Programming
paradigm or OOPs, are known as Object-Oriented Programming languages. Some of the
major Object-Oriented Programming languages include:
Java
C++
Javascript
Python
PHP
Nearly all programming paradigms include Structured programming, including the OOPs
model.
Inheritance
Encapsulation
Polymorphism
Data Abstraction
1. What is a class?
For example, first, a car’s template is created. Then multiple units of car are
created based on that template.
2. What is an object?
An object refers to the instance of the class, which contains the instance of the
members and behaviors defined in the class template. In the real world, an
object is an actual entity to which a user interacts, whereas class is just the
blueprint for that object. So the objects consume space and have some
characteristic behavior.
For example, a specific car
3. What is 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.
2) Data binding: Encapsulation is the process of binding the data members and the
methods together as a whole, as a class.
4. What is Polymorphism?
Polymorphism is composed of two words - “poly” which means “many”, and “morph”
which means “shapes”. Therefore Polymorphism refers to something that has many
shapes.
In OOPs, Polymorphism 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.
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.
The term “inheritance” means “receiving some quality or behavior 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 reuse.
8. What is 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.
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.
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.
Constructors are special methods whose name is the same as the class name. The
constructors serve the special purpose of initializing the objects.
For example, suppose there is a class with the name “MyClass”, then when you
instantiate this class, you pass the syntax:
MyClass myClassObject = new MyClass();
Now here, the method called after “new” keyword - MyClass(), is the constructor of this
class. This will help to instantiate the member data and methods and assign them to the
object myClassObject.
Default constructor: The default constructor is the constructor which doesn’t take any
argument. It has no parameters.
class ABC
{
int x;
ABC()
{
x = 0;
}
}
Parameterized constructor: The constructors that take some arguments are known as
parameterized constructors.
class ABC
{
int x;
ABC(int y)
{
x = y;
}
}
class ABC
{
int x;
ABC(int y)
{
x = y;
}
// Copy constructor
ABC(ABC abc)
{
x = abc.x;
}
}
Contrary to constructors, which initialize objects and specify space for them, Destructors
are also special methods. But destructors free up the resources and memory occupied by
an object. Destructors are automatically called when an object is being destroyed.
15. Are class and structure the same? If not, what's the difference between
a class and a structure?
No, class and structure are not the same. Though they appear to be similar, they have
differences that make them apart. For example, the structure is saved in the stack
memory, whereas the class is saved in the heap memory. Also, Data Abstraction cannot
be achieved with the help of structure, but with class, Abstraction is majorly used.
Let me explain to you with a common 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 behavior. 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.
Yes, with more powers comes more complications. Inheritance is a very powerful feature
in OOPs, but it has some limitations too. Inheritance needs more time to process, as it
needs to navigate through multiple classes for its implementation. Also, the classes
involved in Inheritance - the base class and the child class, are very tightly coupled
together. So if one needs to make some changes, they might need to do nested changes in
both classes. Inheritance might be complex for implementation, as well. So if not
correctly implemented, this might lead to unexpected errors or incorrect outputs.
Single inheritance
Multiple inheritances
Multi-level inheritance
Hierarchical inheritance
Hybrid inheritance
The subclass is a part of Inheritance. The subclass is an entity, which inherits from
another class. It is also known as the child class.
Whereas Overriding is a runtime polymorphism feature in which an entity has the same
name, but its implementation changes during execution. For example, Method overriding.
Image
Data abstraction is accomplished with the help of abstract methods or abstract classes.
Interface and abstract classes both are special types of classes that contain only the
methods declaration and not their implementation. But the interface is entirely different
from an abstract class. The main difference between the two is that when an interface is
implemented, the subclass must define all its methods and provide its implementation.
Whereas in object-oriented programming, when a subclass inherits from an abstract class
with abstract methods, the subclass is generally required to provide concrete
implementations for all of those abstract methods in the abstract class unless the subclass
itself is declared as abstract.
Also, an abstract class can contain abstract methods as well as non-abstract methods.
Access specifiers, as the name suggests, are a special type of keywords, which are used to
control or specify the accessibility of entities like classes, methods, etc. Some of the
access specifiers or access modifiers include “private”, “public”, etc. These access
specifiers also play a very vital role in achieving Encapsulation - one of the major
features of OOPs.
An exception can be considered as a special event, which is raised during the execution
of a program at runtime, that brings the execution to a halt. The reason for the exception
is mainly due to a position in the program, where the user wants to do something for
which the program is not specified, like undesirable input.
No one wants its software to fail or crash. Exceptions are the major reason for software
failure. The exceptions can be handled in the program beforehand and prevent the
execution from stopping. This is known as exception handling.
So exception handling is the mechanism for identifying the undesirable states that the
program can reach and specifying the desirable outcomes of such states.
Try-catch is the most common method used for handling exceptions in the program.
Object-oriented programming revolves around entities like objects. Each object consumes
memory and there can be multiple objects of a class. So if these objects and their
memories are not handled properly, then it might lead to certain memory-related errors
and the system might fail.
Garbage collection refers to this mechanism of handling the memory in the program.
Through garbage collection, the unwanted memory is freed up by removing the objects
that are no longer needed.
32. Can we run a Java application without implementing the OOPs
concept?
However, on the other hand, C++ can be implemented without OOPs, as it also supports
the C-like structural programming model.