C# and .NET OOP (Object oriented programming) interview questions - Abstract classes and interfaces.
I have yet to remember a .NET interviewer who never asked about abstract classes , interfaces and object oriented interview questions . I am putting forward questions which comes around abstract classes and interfaces again and again...Hope every one benefits.
Normally the C# interviewer starts with.....
What is abstract class ?
Abstract class is a base class or a parent class. Abstract classes can
have empty abstract methods or it can have implemented methods which can be
overridden by child classes.
The next question i expected was on interfaces and yes there it comes.
What are interfaces?
Interface is a contract class with empty methods , properties and functions. Any class which implements the interface has to compulsory implement all the empty methods , functions and properties of the interface.
Now the 1000% sure question was bound to come difference between them...
What's the difference between abstract class and interface?
There are many differences, below are some key two differences :-
- Abstract class are base class or parent class while interfaces are contracts.
- Abstract class can have some implemented methods and functions while interfaces methods and functions are completely empty.
- Abstract classes are inherited while interfaces are implemented.
- Abstract classes are used when we want to increase reusability in inheritance while interfaces are used to force a contract.
Can we create a object of abstract class or interface?
No we can not.
Now the practical question..
In what scenarios will you use a abstract class and in what scenarios will you use a interface?
If you want to increase reusability in inheritance then abstract classes are good. If you want implement or force some methods across classes must be for uniformity you can use a interface. So to increase reusability via inheritance use abstract class as it is nothing but a base class and to force methods use interfaces.
Really friends having C# experience is one thing and cracking
Dot net and C# interviews is a different ball game all together .
QuestPond subscription program
Call: +91 9967590707
Email: firstname.lastname@example.org ,
Call: +91 9967590707
FOR SALES IN USA & CANADA