- If any class is implementing an interface that should provide implementation for all the methods which defined in Interface.
- Class can implement multiple Interfaces.
- Derived class must provide implementation for all abstract methods defined in abstract class.
A class may implement one or more interfaces. | A class may inherit only one Abstract Class or any other class |
An interface cannot have access modifiers for any types declared in it. By Default all are public. | An abstract class can contain access modifiers. |
If various implementations only share method signatures then it is better to use Interfaces. | If various implementations are of the same kind and use common behavior or status then abstract class is better to use. |
Requires more time to find the actual method in the corresponding classes. | Fast |
All methods declared in interface must be implemented in derived class. | Only abstract methods need to be implemented in derived classes. |
If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. | If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code will work without any modification. |
No fields can be defined in interfaces | We can define fields and constants in abstract class. |
Can I declare abstract members as protected or internal in abstract class?
Ans: Yes.
Can I declare abstract members as private in abstract class?
Ans: No Virtual or Abstract members can not be declared as private.
Below Code demonstrates the Interface and abstract classe Usage:
Abstract Class Definition:
public abstract class Employee
{
// we can declare fields and properties
protected String emp_id;
protected String emp_lname;
protected String emp_fname;
// properties
public abstract String ID
{
get;
set;
}
public abstract String FirstName
{
get;
set;
}
public abstract String LastName
{
get;
set;
}
//methods
public String Add();
{
return "Employee " +
emp_id + " " + lname + " "+ fname
+" "added";
set;
}
//abstract method, derrived class must provide
implementation
public String CalcWage();
}
public intereface IEmployee
{
// we can not declare fields
//we can define just signature of the properties .
String ID
{
get;
set;
}
String FirstName
{
get;
set;
}
String LastName
{
get;
set;
}
//no method implementation
String Add();
String CalcWage();
}
public class Emp_FullTime: Employee
{
// //uses all the properties and fields of abstract class
//we can define just signature of the properties .
public Emp_FullTime()
{
}
public override string ID
{
get{ return emp_id; }
set{ emp_id = value; }
}
public override string FirstName
{
get {return fname; }
set {fname=value; }
}
public override string LastName
{
get {return lname; }
set {lname=value; }
}
//common methods that are implemented in the abstract class
public new string Add()
{
return base.Add();
}
public oberride string CalcWage()
{
return base.Add();
}
String CalcWage()
{
return "Full time employee "
+ + base.fname + "is
calculated " + "Using the abstract class ";
}
}
public class Emp_FullTime2: IEmployee
{
protected string emp_id;
protected string emp_lname;
protected string emp_fname;
public Emp_FullTime2()
{
}
public string ID
{
get{ return emp_id; }
set{ emp_id = value; }
}
public string FirstName
{
get {return fname; }
set {fname=value; }
}
public string LastName
{
get {return lname; }
set {lname=value; }
}
public string Add()
{
return "Full time Employee
" + fname + " calculating using " + "Interface...";
}
public oberride string CalcWage()
{
return base.Add();
}
String CalcWage()
{
return "Full time employee "
+ base.fname + "is
calculated " + "Using the abstract class ";
}
}
Yes. We can implement method by prefix with Interface name.
For Ex:
public interface Interface1
{
int Add();
}
public interface Interface2
{
int Add();
}
Interface1 and Interface2 having same method Add(), If we implement both of these, the implementation would be like this.
public class :Interface1,Interface2
{
Interface1.Add()
{
//Code Here
}
Interface2.Add()
{
//Code Here
}
}
How can we access implemented methods from outside of the class?
Test obj=new Test();
Interface1 obj1=obj;
Obj1.Add(20,30); // to call Interface1.Add() method
Interface2 obj2=obj;
Obj2.Add(20,30); // to call Interface2.Add() method