Top 30 OOPS Interview Questions and Answers
Top 30 OOPS Interview Questions and Answers
What is a class?
A class is nothing more than a description of an object. It is a model, design, or prototype that
describes an object’s features.
What is an object?
An object is termed as an instance of a class, and it has its own state, behavior, and identity.
Define a constructor.
A constructor is a method used to initialize the state of an object, and it gets invoked at the time
of object creation.
What is a superclass?
A superclass or base class is a class that acts as a parent to some other class or classes.
For example, the Vehicle class is a superclass of Class Car. or, BMW is a type of car, so the
superclass for BMW is Class Car.
overloading overriding
two or more methods have same name child class redefining methods present in the base class or
but different parameters or signatures parent class with the same parameters/signatures
resolved during compile time resolved during run time
What is the use of ‘finalize’?
Finalize is a method used to free up unmanaged resources and cleanup before Garbage
Collection(GC). It performs memory management tasks.
A try/ catch block is used to handle exceptions. The try block defines a set of statements that
may lead to an error. The catch block basically catches the exception.
What is Encapsulation?
Encapsulation is a feature of an entity that holds all secret data. The members of that class can
only see the hidden details. Public, Protected, Private are the different levels.
Inheritance is a concept where one class shares the structure and behavior defined in another
class. Inheritance applied to one class is called Single Inheritance, and if it depends on multiple
classes, then it is called multiple Inheritance.
Uses:-
Static Binding is a binding in which the name can be combined with the class during
collection time, and it is also called early binding.
Dynamic Binding is a binding in which name can be identified with the class during
execution time, and it is also known as Late Binding.
Can you call the base class method without creating an instance?
It is a static method.
The base class is inherited by some other subclass.
How Can We Call The Base Method Without Creating An Instance?
Yes, it is possible to call the base method without creating an instance. And that method should
be the static method. Doing inheritance from that class use the Base Keyword from a derived
class.
Constructors Methods
constructor name should match with the name of the Methods should not have the same name
class as the class name
used to execute certain statements written
used to initialize and allocate memory of to the object
inside them
constructors are invoked by the system whenever
invoked when it is called
objects are created
they are invoked using new keyword while creating an
invoked during program execution
instance of the class
does not have a return type has a return type
cannot be inherited by subclass can be inherited by a subclass
Abstract Class:-
Abstract Method:-
Extends Implements
A class can extend another class interface A class can implement an interface
A subclass extending superclass may not A class implementing interface has to implement
override all of the superclass methods all the methods of the interface
A class can only extend a single superclass A class can implement any number of interfaces
An interface can extend more than one An interface cannot implement any other
interfaces interface
Syntax: Class child Extend Class Parent Syntax: Class Hybrid implements Rose
What is a need for Object-oriented programming?
What is a destructor?
It is a method which is called when the object is destroyed. Destructor name is the same as class
name with “~” symbol before the name.
Virtual function is a special member function that is declared within a base class and redefined
by derived class to go with its own task. It is declared using a token, and is implemented by
using the virtual keyword.
Abstract classes are special classes which are declared but are initialized, which means that it is
not possible to create an object for the abstract class.
Access specifiers are used to hide or show data to the end user. It is used in data hiding or data
abstraction.
What is casting?
In C++ we create a pointer reference between parent and child classes and form an “is a”
relationship between them, This is known as casting and they are of two types:-
Upcasting
Downcasting
Run time polymorphism helps resolve the problem of early binding at compile time.
#include <stdio.h>
using namespace std;
// This is Base class
class Base
{
public:
virtual void show()
{
cout << "Showing Base Class" << endl;
}
};
// This is Derived class function
class Derived : public Base
{
public:
// This function is declared in the base class as well
void show()
{
cout << "Showing Derived Class" << endl;
}
};
int main()
{
// pointer reference to base class
Base *base_object;
// creating derived class object
Derived derived_object;
// mapping base object to address of derived class object
base_object = &derived_object;
// since we use pointers so we use -> rather than .
base_object->show();
return 0;
}
Access modifiers are used to figure out the scope of the method or variables accessible from
other various objects or classes.
Private
Public
Protected
Friend
Protected Friend
What is Abstraction?
Abstraction refers to revealing only the most important information while concealing the details.
Data abstraction refers to exposing only the most important aspects of the data to the outside
world while concealing the implementation information.
Abstraction Encapsulation
Gives a class a general structure and Creates and defines an object’s permissions and
leaves the implementation to the constraints as well as the variables and methods that
implementers. make up the object.
Encapsulation is accomplished by the use of four
Abstraction is accomplished by the use
different access level modifiers: public, protected, no
of an interface and an abstract class.
modifier and private.
Class Structure
Class is a referencing type. Structure is a value type.
CLR allocates memory for its
Memory is allocated on the stack.
instance in heap.
Support Inheritance. Does not support Inheritance.
Variables of a class can be assigned
Structure members cannot have null values.
as null.
Class can contain Structure does not require constructor/destructor and
constructor/destructor members can be initialized automtaically.
A parameter is a variable used during the declaration of the function or subroutine and arguments
are passed to the function, and it should match with the parameter defined.
Call by Value – Value passed will get modified only inside the function, and it returns
the same value whatever it is passed into the function.
Call by Reference – Value passed will get modified both inside and outside the functions
and it returns the same or different value.
The following are some of the similarities between a class and a structure:
Access specifiers, such as public, private, and protected, are identically used in structures
and classes to restrict the access of their data and methods outside their body.
The access level for class members and struct members, including nested classes and
structs, is private by default. Private nested types are not accessible from outside the
containing type.
Both can have constructors, methods, properties, fields, constants, enumerations, events,
and event handlers.
Both structures and classes can implement interfaces to use multiple-inheritance in code.
Both structures and classes can have constructors with parameters. Both structures and
classes can have delegates and events.
Java is not an entirely pure object-oriented programming language. The following are the
reasons:
Java supports and uses primitive data types such as int, float, double, char, etc.
Primitive data types are stored as variables or on the stack instead of the heap.
In Java, static methods can access static variables without using an object, contrary to
object-oriented concepts.
Encapsulation is a feature of an entity that holds all secret data. The members of that class can
only see the hidden details. Public, Protected, Private are the different levels.
Single Inheritance: Single child class inherits characteristics of the single-parent class.
Multiple Inheritance: One class inherits features of more than one base class and is not
supported in Java, but the class can implement more than one interface.
Multilevel Inheritance: A class can inherit from a derived class making it a base class
for a new class, for example, a Child inherits behavior from his father, and the father has
inherited characteristics from his father.
Hierarchical Inheritance: One class is inherited by multiple subclasses.
Hybrid Inheritance: This is a combination of single and multiple inheritances.
What is Interface?
In general, an interface is a system that allows two or more unrelated entities to communicate.
According to this definition, remote control is an interface between you and a television set, the
English language is an interface between two people, and the protocol of behavior enforced in
the military is the interface between people of different ranks.
Within the Java programming language, an interface (in the glossary) is a type, just as a class is a
type. Like a class, an interface defines methods. Unlike a class, an interface never implements
methods; instead, classes that implement the interface implement the methods defined by the
interface. A class can implement multiple interfaces.
Default Constructor: The default constructor is the constructor which does not take any
argument. It has no parameters.
class A
{
int x;
A()
{
x = 0;
}
}
Parameterized constructor: The constructors that take some arguments are known as
parameterized constructors.
class A
{
int x;
A(int y)
{
x = y;
}
}
Copy constructor: A copy constructor is a member function that initializes an object using
another object of the same class.
class A
{
int x;
A(int y)
{
x = y;
}
// Copy constructor
A(A a)
{
x = a.x;
}
}