Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Oct 9, 2021, 2:02 AM EDT
An interface has only abstract methods (method without body) but an abstract class can have both abstract as well as non abstract methods (methods with body), so it can also support access modifiers (private,internal etc) but interface has by default public methods only.
so, it is a common practice that whenever you have to create contract (force child class to implement and use same naming conventations) we use interface.
Another benefit of interface is to achieve loose coupling between two projects or classes. we use interface instead of concrete objects which also hides the real implementions from caller class.
public class EmployeeBL
{
private IDepartmentBL _DepartmentBL;
public Employee(IDepartmentBL DepartmentBL)
{
_DepartmentBL=DepartmentBL;
}
public Department DepartmentByEmployeeId(int id)
{
return _DepartmentBL.DepartmentByEmployeeId(id);
}
}
so, caller doesn't know which implementation out of the given two implementions, _DepartmentBL pointing to, and will be resolved at run time using dependency injection.
public Interface IDepartmentBL
{
Department DepartmentByEmployeeId(int id);
}
public class DepartmentBL1 :IDepartmentBL
{
public Department DepartmentByEmployeeId(int id)
{
return Departmets.Where(x=>x.EmployeeId==id).singleOrDefault();
}
}
}
public class DepartmentBL2 :IDepartmentBL
{
public Department DepartmentByEmployeeId(int id)
{
return Departmets.Where(x=>x.EmployeeId==id).singleOrDefault();
}
}
}
Rijwan AnsariRank #82 · 24.2K reputationPosted Oct 12, 2021, 11:00 AM EDT
Bohdan StupakRank #479 · 5.2K reputationPosted Oct 14, 2021, 11:44 AM EDT
Srinivasan RamamoorthiRank #877 · 2.5K reputationPosted Oct 10, 2021, 12:36 AM EDT
Satya KarkiRank #78 · 24.6K reputationPosted Oct 12, 2021, 12:45 PM EDT
satheesh d190 reputationPosted Nov 1, 2021, 9:35 AM EDT
Abstract class is one which you cannot be instantiate but inheriatnce is possible. This abstract class can only be used a base class. the non abstract class derived from abstract class must provide the implementation for all inherited abstract members.
public abstract class customer
{
public abstract void print();
}
public class program : customer
{
public override void print()
{
Console.WriteLine("printed");
}
}
Interface is also like classes which contains methods,properties but it is only declared and not implemented. Interface by default is public. the class which inherits this interface must provide implementation for all interface members. another advantages, it can able to inherit more than one interface at a same time.
Interface Icustomer
{
void print();
}
public class Employee : Icustomer
{
public void print()
{
Console.WriteLine("Printed");
}
}
public class program
{
public static Main()
{
Employee emp =new Employee();
emp.print();
}
}
Differences: The usage is depends upon the requirements.The interface can inherit more than one interface at a same time but not abstract class. An inteface cannot inherit from abstract class. But abstract class can be inherited from interfaces.
Jignesh KumarRank #91 · 22.9K reputationPosted Nov 1, 2021, 9:59 PM EDT