OOPs Interview Que Ans
OOPs Interview Que Ans
OOPs or Object Oriented Programming mainly comprises of the below four features,
and make sure you don't miss any of these:
Inheritance
Encapsulation
Polymorphism
Data Abstraction
What is a class?
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.
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.
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.
Example:
// In this program, we will see how multiple functions are created with the same
name,
// but the compiler decides which function to call easily at the compile time
itself.
class CompileTimePolymorphism{
// 1st method with name add
public int add(int x, int y){
return x+y;
}
// 2nd method with name add
public int add(int x, int y, int z){
return x+y+z;
}
// 3rd method with name add
public int add(double x, int y){
return (int)x+y;
}
// 4th method with name add
public int add(int x, double y){
return x+(int)y;
}
}
class Test{
public static void main(String[] args){
CompileTimePolymorphism demo=new CompileTimePolymorphism();
// In the below statement, the Compiler looks at the argument types and decides
to call method 1
System.out.println(demo.add(2,3));
// Similarly, in the below statement, the compiler calls method 2
System.out.println(demo.add(2,3,4));
// Similarly, in the below statement, the compiler calls method 3
System.out.println(demo.add(2,3.4));
// Similarly, in the below statement, the compiler calls method 4
System.out.println(demo.add(2.5,3));
}
}
In the above example, there are four versions of add methods. The first method takes
two parameters while the second one takes three. For the third and fourth methods,
there is a change of order of parameters. The compiler looks at the method signature
and decides which method to invoke for a particular method call at compile time.
class AnyVehicle{
public void move(){
System.out.println(“Any vehicle should move!!”);
}
}
class Bike extends AnyVehicle{
public void move(){
System.out.println(“Bike can move too!!”);
}
}
class Test{
public static void main(String[] args){
AnyVehicle vehicle = new Bike();
// In the above statement, as you can see, the object vehicle is of type
AnyVehicle
// But the output of the below statement will be “Bike can move too!!”,
// because the actual implementation of object ‘vehicle’ is decided during
runtime vehicle.move();
vehicle = new AnyVehicle();
// Now, the output of the below statement will be “Any vehicle should move!!”,
vehicle.move();
}
}
As the method to call is determined at runtime, as shown in the above code, this is
called runtime polymorphism.
What is Abstraction?
Abstraction is the method of hiding unnecessary details from the necessary ones. It is
one of the main features of OOPs.
What is a constructor?
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;
}
}
Copy constructor: A copy constructor is a member function that initializes an object
using another object of the same class.
class ABC
{
int x;
ABC(int y)
{
x = y;
}
// Copy constructor
ABC(ABC abc)
{
x = abc.x;
}
}
What is a destructor?
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.
What are the various types of inheritance?
Single inheritance
Multiple inheritances
Multi-level inheritance
Hierarchical inheritance
Hybrid inheritance
What is a subclass?
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.
Define a superclass?