OOPS interview questions and answers

In this article, I’ll try to cover all the OOPS questions you must know before going for an interview. OOPS (Object-oriented programming) refers to languages that use objects in programming. Object-oriented programming (OOPS) aims to implement real-world entities like encapsulation, inheritance, polymorphism.

1. What is Polymorphism?

a. Polymorphism basically enables you to invoke derived class methods through base class reference variable at runtime.
b. A base class reference variable can hold the child class object.
c. If we do not override the virtual method then at runtime it will pick the default implementation.

BaseClass bc = new DerivedClass();

There are two types of Polymorphism:

1. Runtime Polymorphism (also known as Method-overriding)
2. Compile Polymorphism (also known as Method-overloading)

2. What is the difference between Method overriding and Method overloading?

Method Overriding:

When we have a virtual method in the base class and override that method in a derived class.

Note: To hide the method we can use the new keyword.
Example:
public new void Print()
{
}
In this class base class function is called.

Method Overloading:

Method overloading is when we have multiple methods the same name but with a different signature. The signature of the method consists of the name of the methods and the type.

3. What is the difference between Encapsulation and Abstraction?

a. Abstraction is showing what is necessary and encapsulation in hiding complexity.
b. Identified what is system is an abstraction.

Example of hiding complexity:

void Add()
{
Validate();
ConnectDatabase();
}

4. What is an Abstract Class?

a. Abstract class can’t be initiated.
b. Abstract class can’t be sealed.
c. An abstract class can only be used as a base class
d. A non-abstract class derived from an abstract class must provide an implementation for all the inherited abstract members.
e. An abstract class may contain a non-abstract method.

5. Can abstract class have a constructor?

Yes, Abstract class may have a constructor. We can use this constructor when we want to initialize anything before a derived class constructor is called.

* To make a default implementation in abstract/base class we can use a virtual and override keyboard.

6. Why and when to use Abstract class?

a. To prevent the developer to create an instance of a base class.
b. We can declare abstract methods so that a developer must have to provide the implementation when deriving an abstract class.

7. Why multiple inheritance is not supported in c#?

Because of the diamond problem.

oops interview question
diamond problem

If the method in D calls a method to define in A (and doesn’t override the method) and B and C has overridden that methods then from which class does it inherit B or C?

This ambiguity is called a diamond problem.

Related Post: C# interview questions

Leave a Comment

Your email address will not be published. Required fields are marked *

%d bloggers like this: