Site icon SKLEARNING

OOPS INTERVIEW QUESTION

java-oops

Why do we need object oreinted programming ?

object oreinted programming helps us to think, in terms of real world objects. Let us try to understand this statement thinking in terms of real world objects. You are creating a software for a hospital, a hospital has patients and now in the code ,you will create class patient, patient has a name, address. Hospital has doctors, will say class doctor. Doctor has a name the patient is allocated to a doctor or doctor treats a patient. This is the doctor object aggregated inside it.

Your code is mimicking the real world object. Patient has a name address, there is a doctor who is allocated to a patient. By having such approach you can manage your code better, your code gets

organized properly. Object Oriented Programming helps us to think in terms of real world objects.

As we complete the first question’s answer lets see one more important P about technical interview

that is be Practical. Technical interview should be substantiate with the practical examples. We just saw object oriented programming helps us to think in terms of real world objects and later on gave an example of a hospital, patient, doctor I talked about aggregation. Just try to support your theoritical answers with some practical examples as well. This send message to the interviewer that you have worked with an enterprise application and you are person for this job.

One serious request when it comes to object oriented programming don’t give examples about car, dog, cat, tree because it can send a wrong message to the interviewer, it can make the interview very weird. Always try to give examples like inventory, payroll, billing, accounting, hospital management system or something of domain you have worked with.

 what are the important pillars in OOP ?

There are four important pillars in oop,

The first one is Abstraction:

Abstraction means show only what is necessary.

Second one Polymorphism –

object can act differently under different conditions so user object can become an employee or can become an admin or worker. Object can take different shapes in different conditions or it act differently under different condition.

Third one inheritance :-

you can have a parent child relationship where you have something common in the parent and the child can inherit and add something more to it.

Last one is Encapsulation:

means Hide complexity. Hide anything which is complex which should not be shown out side the object.

 

Incase you do not remember these 4 things remember the word APIE –
A for Abtraction, P for Polymorphism, I for Inheritance and E for Encapsulation. Do not

it is okay to use acronyms. You are working in a company, doing some practical work, sending emails

The whole day you are not revising and learning. Doing a job and cracking a interview are two different things. A prepared developer has more chance of clearing an interview than an unprepared superhero. You can be a superhero in a company, would have delivered big projects but if you do not answer simple questions like what are the important characteristics of OOP

it is difficult to clear the interview. For this question remember this acronym APIE.

Cracking a technical interview is 70%

you should have a knowledge of the topic but 30% is about your technical vocabulary.

 what exactly is the class and object?

A class is a blue print a class is type.

Class has Employee, it has name, address and it can have some functions, methods. Now to use this class we need to create an instance where will say Employee e1 = new Employee();

Here e1 is an object it is an instance or can create one more instance e2 = new Employee();

We can say e1 name is Shiv and e2 name is Raju. We can say a class is a blue print and to use the class we need to create the instance of the class which is an object.

Differentiate between Abstraction and Encapsulation.

The answer to this abstraction means show only what is necessary and Encapsulation means hide complexity.

The answer is so synonymically same, it is like saying we are happy when we drink beer or drinking bear make us happy. Again the interviewer will counter question saying

what’s the difference between abstraction and encapsulation?

Abstraction says show only necessary for example this is an employee class and in the employee class the name has to be shown outside so we made it public. Address has to be shown outside we made it public and then this is the validate function which has ton be shown outside we made it public.

When you say validate you would like to validate the name and the address.

You create two more function or rather i will say developer will create two more functions like checkname which will actually check the name CheckAddress which will check the address.

Inside the validate function you will have checkname and you will have CheckAddress. For now you only want to show validate outside and not CheckName and CheckAddress, they are more internal things. Developer has created it. When he started coding developer has created both these functions so that the validate function can be completed. What happens because of making everything public in abstraction or we decided that for the employee class only validate should be seen outside but we are also seeing CheckName and CheckAddress, we make these as private. Now I only see what is necessary i.e the name , address, validate but I don’t see CheckName and CheckAddress.

Abstraction is a design thought process or it happens during the design phase. When you get the requirement in your technical document. In your class diagram you you decide what are the public methods,

public properties and public clases which have to be exposed outside.

During implementation or during the coding phase the developer needs more functions to complete the abstraction or the thought process. At the time the developer creates more functions and he needs to use the access modifiers like private, public and protected to implement Encapsulation. Abstraction happens during the design phase.

In the design phase you decide what has to be shown public and during execution phase

developer uses encapsulation means he uses private, public and protected access modifiers

to implement the thought process of abstraction. One more thing which you

can really put ahead before the interviewer is Encapsulation implements abstraction.

Encapsulation and Abstraction complement each other. When you give the answer just try to emphasize that Encapsulation implements Abstraction and abstraction happens during design phase and encapsulation happens during the coding phase.

Explain Inheritance –  Inheritance is one of properties of object oriented programming wherein you can define a parent and a child relationship between two classes.

This is an employee class out here you can create a new class called as manager. This manager will have all the properties and the methods of the parent class. To do inheritance will say Manager inherits from Employee so now you get all the properties of the parent class methods and everything but you can define your own methods in the child class, I can say here manager does management. If I create the employee class I will get x properties but if i create the object of the manager class i will get all the properties and the methods of the parent class

but as well as the manager class has its own extra methods like management. Inheritance defines a parent and a child relationship between two classes. In the interview lot of times this question can also tweet and interviewer can ask what is a relationship is a relationship again a parent child relationship ? Is a relationship means manager is a child of employee. Lot of times this inheritance question is also tweet by asking what is a relationship?

What is the use of virtual keyword or what exactly is virtual methods?

This question can also come in a tweet manner where interviewer can ask explain the concept of overriding, virtual keyword, virtual methods the answer lies in the same concept. Virtual keyword helps us to define some logic in the parent class which can be overridden in the child class. We have this class employee we can say this method as virtual. As soon as you give this word virtual here go to your child class. For example this manager is inheriting from the employee so the manager becomes a child class. We can say override validate. The employee class will have certain functionality but we can override here with our own logic. Virtual keyword helps us to provide some implementation in the parent class which can be overridden in the child class. This concept is also termed as overriding.

One of the connected questions which comes with Overriding is what exactly is Method overloading ?

This question can come in a tweet fashion what is the difference between overriding and overloading?

Method overloading means same method names with different signature.

We have a validate method out here and will have one more method called as validate and it has an input. This is validate without any inputs, this is validate with the input. I can have one more validate web I say some another input.

The validate method in this manager class has been overloaded three times.

One is it has validate with without any signature, another one is it has validate which has a boolean input and there is an another validate which it has two more inputs. Method overloading means in the same class you can have same method names but different signatures. You can say here validate without the input, validate with the boolean input and validate with two more inputs. This is termed as method overloading.

Quickly summarize the difference between an Overloading and Overridding. This question is like comparing Applie with Mangoes but let us try to put some sentence around it.

When you talk about method overloading then in the same class you have the same method names with different signatures. Method overriding comes in a parent child relationship so there should be a parent child relationship or else method overriding does not exists.

We have an employee class it has a validate method which is marked as virtual then this virtual method is later overridden by using the override keyword. In method overriding we are using this override keyword and there is a parent child relationship where the parent class has a virtual method. Method overriding makes sense when we talk about parent child relationship, when you talk about a parent method

overloading in the same class will have the method names with different signature.

Will talk about what is Polymorphism, what is static polymorphism, what is dynamic polymorphism, what is operator overloading and so on.

Explain polymorphism?

If you see the word polymorphism, poly means many and morph means change as per situation. It is ability of an object to act differently under different conditions, the ability of an object to act differently under different condition.

Can we implement polymorphism without inheritance or without parent child relationship?

The answer is NO, this question specifically from the interviewer from the point do you understand polymorphism in detail.

To implement polymorphism inheritance is must.

What are the two kinds of polymorphism in C# ?

The two kind of polymorphism in C# are Static and Dynamic polymorphism.

Many developer also term them as run time polymorphism and compile time polymorphism.

Run time polymorphism means dynamic polymorphism and compile time is static polymorphism.

Static polymorphism is implemented by method overloading while dynamic polymorphism is implemented by overriding. We have this Manager class, validate method.

One validate method we have without a signature then we have validate method with a one signature

and then we have validate method with two signatures. One signature is static polymorphism,

this will be checked during the compile time means when I hit the build at that time it will check.

Runtime polymorphism and dynamic polymorphism happen in a parent child relationship.

In parent child relationship we have to use virtual keywords

and then the manager goes and inherits and overrides this validate. I can say dynamic polymorphism employee e can point towards manager and it will call e.validate

and the same employee can point towards supervisor and and he can call validate.

Method overriding is run time polymorphism and method overloading is static polymorphism.

Explain operator overloading?

Operator overloading is a concept of polymorphism where we can redefine operators like plus sign and the minus sign and the multiplication sign with addition functionalities. It is inbuilt inside .net by default. When you use the + sign, this . This will concatinate both the strings. If we say 1+2

then this will do an arithmetic addition. By default you will see polymorphism in .NET.

The next cross question from the interviewer can be what if you want to implement your own logic of polymorphism or do a custom operator overloading. Here I am treating an object of some class called as o1 and creating another class object as o2. What if i want to add

these objects o1 & o2, for that us the operator overloading keyword here operator and the + sign.

This complete method we need to define as static public static SomeClass operator

and pass the first instance of SomeClass and second instance of SomeClass

and then you define the logic what you want to do. I am adding this 2 numbers of SomeValue.

To do a custom operator overloading then use operator keyword. If I run this application you will see two objects will be added where o1 + o2 will give o3 and in o3 we have 10+20 which is 30. The value in the o3 object of someValue is 30.

Operator overloading means the ability to load an operation without functionalities and to do custom operator overloading then use the operator keyword.

 

Between Abstract classes and interfaces abstract class is easy one. Let us start with some questions around abstract classes first then go towards interfaces and will compare the differences between abstract class and interface. What is an abstract class?

Abstract class is a half defined parent class or a partially defined parent class.

Here I have a customer class then from this customer class we have two other classes which are inheriting GoldCustomer and SilverCustomer.

Both the GoldCustomer and the SilverCustomer share lot of common properties for example they share

the customer name, product name, address, productAmount. There is a CalculateDiscount

and this CalculateDiscount is overridden by both of the classes with their own behaviour. GoldCustomer is getting 10% of discount and SilverCustomer is getting 5% of discount. In the parent class because we do not have implementation we have said here throw new

NotImplementedException, in other words if anybody tries to invoke this calculate discount of the parent class it will say this will be done by the child classes, there is no

implementation out here. The problem with this approach is that somebody can

create an object of a Customer class like this and he will try to invoke CalculateDiscount.

We can have confusion because you have customer class so developers

will try to create the instance of the customer class, they will try to invoke this CalculateDiscount and they will get this NotImplementedException which is really bad. Over here there is no implmentation. In this customer class there is

some implementation and some of the implementation has to be defined by the child class. We need to

create a class which is half defined, partially defined so for that we need abstract class.

We can say abstract class by saying abstract. This method CalculateDiscount is without any logic so it should be half in other words there should not be any logic out there we say this as an abstract.

This customer class now is half defined. The name, address, productName and Amount is fully defined

but the calculate discount will be overridden by the child classes and the logic will be defined.

The best part about the abstract class is that it is half defined you cannot create instance of it.

The confusion where you can invoke a half class then later on it give an error, it is stopping you to not to create instance which is actually very good. What you have out infront of the interviewer

is Abstract class is a half defined parent class where there are some implementation

which is defined and some implementationis left to the Child classes to be defined.

One more tricky question which comes around abstract classes are abstract methods of an

abstract class virtual? The answer is yes. In the last OOP interview questions I talked about the virtual keyword. Virtual keyword what it does is, it will define in a parent class method as

virtual like this, in the child class you can override it. In the child class you can say override. The CalculateDiscount is seen because it is virtual, if I remove this word virtual

i will not see CalculateDiscount. The question here the interviewer asking is if I declare a method as abstract in abstract class yes it is virtual becuase down below you can say override and see CalculateDiscount. If you do not declare this as an abstract it will not be seen.

Abstract method in abstract class are by default virtual. There can be some short questions around abstract classes which can be asked

and those answers are just Yes and No but be very careful when answering them. First short question

which comes around abstract classes is that can we create instance of an abstract class? absolutely

NO. You cannot create an instance of an abstract class, the compiler will throw up an exception.

When I say NewCustomer it says you cannot create an instance of an abstract class that is the first short question. The second short question which is asked around abstract class is is

it compulsory to implement the abstract methods in the child class? We have CalculateDisocunt, is it

compulsory that we should implement those methods in the child classes? yes it is compulsory, these methods are half defined so if we do not write a code in a child class it will give an exception saying the GoldCustomer does not implement CalculateDiscount. If there is an Abstract Method or Abstract Function that it has to be implemented compulsarily in the child class.

Once more crossed question which can be asked around the abstract class is why can’t a simple base or parent class replace an abstract class? A simple base class or simple parameter class we cannot define it in half way, we cannot define it partially. In other words for example If we make it a simple class then we need to write some kind of a hack logic, will say here return Null or return 0 or will say throw NewNotImplementedException.

It is not a clean way of creating a half partial class or half base class,

its like a hack but when we actually create an abstract class this becomes a pure partial class. Somethings are defined and somethings are not defined.

Exit mobile version