I am here to continue the series related to .NET interview preparation. Today we will discuss the questions related to Abstract Class & Interface and present the answers in an easy way.
- Preparing .NET Interview - Part One (Framework)
- Preparing .NET Interview - Part Two (Basic Types)
- Preparing .NET Interview - Part Three (Parameters)
- Preparing .NET Interviews - Part Four (Operators)
- Preparing .NET Interview - Part Five (Equality)
- Preparing .NET Interview - Part Six (OOPs)
-
What is an abstract class in .NET and when it is used?
Answer:
Abstract class is a high-level or incomplete class as one or more methods are only declared and left to be implemented by derived classes. These just declared or unfinished methods need to be marked as abstract. Abstract class and members need to follow few rules as follows.
• Instance of an abstract class can’t be created.
• Abstract class can’t be marked as sealed as it will stop it from implementing.
• If any class has at least one abstract method, then the class needs to be declared as abstract.
• Abstract methods when implemented in derived classes, need to mark as an override.
Example:
Output:- abstract class BaseClass
- {
- public abstract float CalculateDiscount(float premium, float regularDiscount, float saleDiscount);
- public void Print()
- {
- Console.WriteLine("Hello Prakash");
- }
- }
- class DerivedClass: BaseClass
- {
- public override float CalculateDiscount(float premium, float regularDiscount, float saleDiscount)
- {
- return (premium * (regularDiscount + saleDiscount)) / 100;
- }
- }
- public class Program
- {
- static void Main()
- {
- Console.Title = "Abstract class demo";
- DerivedClass dClass = new DerivedClass();
- Console.WriteLine("Discount is:{0}", dClass.CalculateDiscount(1000, 10, 2));
- dClass.Print();

-
What is interface and what are the benefits of interface based design?
Answer:
An Interface can be defined as an operations contract in which classes that implement interface must implement all its members.
Methods, Properties, events, and indexers can be the members of an Interface.
Inside a namespace, an interface can be declared as public or internal however its members are by default abstract and public.
Example:
Output:- public interface IEmployee
- {
- int GetEmpId();
- string GetEmpName();
- string GetDeptName();
- void AssignNewProject(int projectId);
- }
- public class Employee : IEmployee
- {
- public int GetEmpId()
- {
- return 101;
- }
- public string GetEmpName()
- {
- return "Prakash";
- }
- //string GetDeptName() //Error as interface members are public by default.
- public string GetDeptName()
- {
- return "Technology";
- }
- public void AssignNewProject(int projectId)
- {
- //Call some service to update the project
- Console.WriteLine("New Project is Assigned successfully");
- }
- }
- public class Program
- {
- static void Main()
- {
- Console.Title = "Interface demo";
- IEmployee emp = new Employee();
- Console.WriteLine("Emp Id: {0}", emp.GetEmpId());
- Console.WriteLine("Emp Name: {0}", emp.GetEmpName());
- Console.WriteLine("Dept Name: {0}",emp.GetDeptName());
- emp.AssignNewProject(1);
- }
- }

It is recommended to keep only the common set of methods in interfaces so that classes that implement them can provide the definition and don’t leave them as “Not-Implemented”.
There are several benefits of interface-based design as following.
• Interface offers object composition or “black box reuse” due to the client only knows the signatures of members and not their implementations.
• Interface provides clear segregation of contract/declaration and implementation.
• Interface enforces loosely coupled design that is easier to reuse, test, and extend.
• Interface by behavior supports runtime polymorphism.
• Passing interface reference in methods is faster than passing concrete objects.
• Test Driven Development (TTD) becomes easy with interface-based design.
-
What’s are the similarities and differences between abstract class and interface?
Answer:
Similarities:
• Both abstract class and Interface are abstract type objects.
• Abstract class and Interface both are not eligible for new instance creation instead their Implementor needs to do the same.
• In general terms, the interface can also be said as a refined abstract class with only public abstract methods.
Differences:
• Abstract class is used when we want to keep a set of common methods to be available for all its derived classes along with some abstract methods whereas interface is used when we want all its members to be implemented by derived classes.
• Class that implements abstract class needs to implement only its abstract members whereas class that implement interface needs to implement all its members.
• Abstract members of abstract classes can be declared as protected, internal and public whereas all the interface members are by default public.
-
What’s the difference between implicit and explicit interface implementation.
Answer:
Please go through my article as following to get the answer.
Hope you have liked the article. Look forward to your comments/suggestions.
Read more articles on .NET:


Prakash TripathiTOP 100Posted May 23, 2016, 9:58 AM EDT
Thnx Debasis.
Debasis SahaTOP 100Posted May 23, 2016, 9:51 AM EDT
Good One..
Prakash TripathiTOP 100Posted May 5, 2016, 2:04 AM EDT
Agree Bhuvanesh.
Bhuvanesh MohankumarPosted May 5, 2016, 1:54 AM EDT
Actually this question is most common in all interviews
Prakash TripathiTOP 100Posted May 4, 2016, 1:40 AM EDT
Thnx Raja.
Raja TPosted May 4, 2016, 1:36 AM EDT
Nice,Thanks for sharing
Prakash TripathiTOP 100Posted May 3, 2016, 6:40 AM EDT
Thnx Amit.
Amit DavePosted May 3, 2016, 6:37 AM EDT
Keep up the Good work!
Prakash TripathiTOP 100Posted May 2, 2016, 11:12 PM EDT
Thnx Humayun.
Humayun Kabir MamunPosted May 2, 2016, 10:50 PM EDT
Nice...
Prakash TripathiTOP 100Posted May 2, 2016, 3:45 AM EDT
Thnx Sonu.
Sonu ChaudharyPosted May 2, 2016, 2:03 AM EDT
Thanks for sharing
Prakash TripathiTOP 100Posted Apr 30, 2016, 10:30 AM EDT
Thnx Kuppurasu
Kuppurasu NagarajTOP 100Posted Apr 30, 2016, 10:20 AM EDT
Nice Sharing..
Prakash TripathiTOP 100Posted Apr 29, 2016, 10:12 PM EDT
Thnx Rahul.
Rahul Kumar SaxenaTOP 100Posted Apr 29, 2016, 9:40 PM EDT
Good Work
Prakash TripathiTOP 100Posted Apr 29, 2016, 8:19 PM EDT
Thnx Thiruppathi.
Thiruppathi RPosted Apr 29, 2016, 6:41 PM EDT
Good Share..
Prakash TripathiTOP 100Posted Apr 29, 2016, 5:33 PM EDT
Thnx Vivek.
Vivek KumarPosted Apr 29, 2016, 12:05 PM EDT
Nice share
Prakash TripathiTOP 100Posted Apr 29, 2016, 9:17 AM EDT
Thnx Ibrahim.
Mohammed IbrahimPosted Apr 29, 2016, 8:45 AM EDT
nice
Prakash TripathiTOP 100Posted Apr 29, 2016, 5:38 AM EDT
Thnx Debasis.
Prakash TripathiTOP 100Posted Apr 29, 2016, 5:38 AM EDT
Thnx Vignesh.
Debasis SahaTOP 100Posted Apr 29, 2016, 5:05 AM EDT
Good One..
Vignesh ManiTOP 100Posted Apr 29, 2016, 4:39 AM EDT
Nice one
Prakash TripathiTOP 100Posted Apr 29, 2016, 3:41 AM EDT
Thnx Gowtham.
Prakash TripathiTOP 100Posted Apr 29, 2016, 3:40 AM EDT
Thnx Hari.
Gowtham RajamanickamTOP 100Posted Apr 29, 2016, 3:39 AM EDT
Nice...
Hari ShankerPosted Apr 29, 2016, 3:32 AM EDT
nice