Why would we use interfaces when we could use abstract classes?
Loading
Why would we use interfaces when we could use abstract classes?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh KumarPosted Nov 2, 2021, 1:59 AM
satheesh dPosted Nov 1, 2021, 1:35 PM
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.
Bohdan StupakPosted Oct 14, 2021, 3:44 PM
Satya KarkiPosted Oct 12, 2021, 4:45 PM
Rijwan AnsariPosted Oct 12, 2021, 3:00 PM
Srinivasan RamamoorthiPosted Oct 10, 2021, 4:36 AM
Sachin SinghPosted Oct 9, 2021, 6:02 AM
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();
}
}
}