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.
So let’s take questions one by one.
-
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:
- 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();
Output:
-
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:
- 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";
- }
-
-
- public string GetDeptName()
- {
- return "Technology";
- }
- public void AssignNewProject(int projectId)
- {
-
- 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);
- }
- }
Output:
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: