Abstract Class Vs Interface is one of the most asked questions in an interview. In this article, we will learn what are the main differences between abstract class and interface. And also we will know in which cases abstract classes and interfaces can fit in our project.
So, let’s start.
Abstract Class in C#
In C# programming language, an abstract class is a way of defining common behaviour. So that other classes can inherit this class for implementation.
Let’s consider a class Shape. We will mark this class as abstract and then we add a method Area()
which is an abstract method:
abstract class Shape
{
public abstract double Area();
}
Next, we create a class Circle
and inherit it from the Shape
class which is our abstract class.
class Circle : Shape
{
public override double Area()
{
// Calculate Area
}
}
Similarly, we can create multiple classes such as Rectangle
, Triangle
, Square
and all these classes inherit the abstract class Shape
. And, this is because we need to calculate the area of different shapes and hence we can use the Area()
method of abstract class Shape
.
So, let’s explore some of the key Properties of Abstract Class in C#.
We can’t instantiate the Abstract Class
Let’s see the syntax to write Abstract class in C#:
public abstract class Employee
{
public int Id { get; set; }
}
Here, we use a keyword abstract
before the class
keyword and this way the class becomes an abstract class.
Now, let’s try to instantiate the abstract class, what does it say?
It shows the error that “Cannot create an instance of the abstract type“
So, we can not instantiate the abstract class, its main use is to serve itself as a base class and for a common set of actions or properties.
Let’s see this in action.
Abstract Class may contain Abstract and Non-Abstract Methods
We have an abstract class EmployeeAbs
. This class contains one property and two methods.
public abstract class EmployeeAbs
{
public string? EmployeeType { get; set; }
public abstract double CalculateSalary();
public double CalculateBonus()
{
return 100000;
}
}
Here, the method CalculateSalary()
is an abstract method and CalculateBonus()
is a non-abstract method. So, an abstract class may contain abstract and non-abstract methods.
Next, we create a class Employee
and inherit this from EmployeeAbs
the class:
public class Employee : EmployeeAbs
{
public override double CalculateSalary()
{
throw new NotImplementedException();
}
}
Here, we implement the abstract class and it adds the implementation of the CalculateSalary()
method only. So, what about the second method i.e. CalculateBonus()
.
Let’s see what will happen when we add the CalculateBonus()
method manually in the Employee class:
public override double CalculateBonus()
{
throw new NotImplementedException();
}
This code will throw a compile time error, saying that: it “can not override inherited member CalculateBonus() because it is not marked as virtual, abstract, or override“
Finally, to implement the CalculateBonus()
method in the Employee
class, we must add a virtual
keyword with the method in the abstract class, just like this:
public abstract class EmployeeAbs
{
public string? EmployeeType { get; set; }
public abstract double CalculateSalary();
public virtual double CalculateBonus()
{
return 100000;
}
}
Now, we can implement both methods in Employee
class and it will not throw any error.
Override keyword
We must use the override
keyword with a method in the child class if we implement it from an abstract class.
We have a class Employee and it inherits from an abstract class EmployeeAbs.
So, here to implement the methods of the abstract class, we use the override
keyword with the methods:
public class Employee : EmployeeAbs
{
public override double CalculateSalary()
{
throw new NotImplementedException();
}
public override double CalculateBonus()
{
throw new NotImplementedException();
}
}
Abstract Method
An Abstract method does not contain a method body. It means they have method declaration only. We use an abstract
keyword with a method to make it an abstract method.
Let’s see the syntax of an abstract method:
public abstract double CalculateSalary();
We can not use abstract keywords with a method which has a method body. So, this code will throw a compile time error:
public abstract double CalculateBonus()
{
return 100000;
}
Error: “The method can not declare the body as it is marked as abstract“
What is an Interface?
An interface defines the contract or blueprint so that different classes can inherit and share common methods and behaviours with loose coupling. An interface may contain properties, methods, and events as its members.
Let’s see the syntax to write an interface in C# programming:
public interface IEmployee
{
}
Key Properties of an Interface
- We can not instantiate the interface. If IEmployee is an interface then we can not write the code like this:
IEmployee employee = new IEmployee();
The code will give an error: “Cannot create an instance of the abstract type or interface“
2. An interface doesn’t contain the method body, it only has a method declaration.
Let’s see this:
public interface IEmployee
{
double CalculateSalary(int empId);
double CalculateBonus(int empId);
}
3. Interface can contain property along with method declaration. Let’s see this in action. We have an interface IEmployee
which contains one property and 2 methods.
public interface IEmployee
{
double TaxAmount { get; }
double CalculateCTC(int empId);
void DisplayTaxAmount(int empId);
}
Now, we will implement the interface. For this, we will create a class Employee
.
public class Employee : IEmployee
{
public double TaxAmount => CalculateCTC(1)*5/100; //Here 1 is the Employee Id
public void DisplayTaxAmount(int empId)
{
Console.WriteLine($"Tax Amount: {TaxAmount}");
}
public double CalculateCTC(int empId)
{
return 500000;
}
}
Significance of an Interface in C# Programming
An interface is good for implementing design patterns and developing reusable, testable and extensible code. An interface can inherit other interfaces to make an interface hierarchy.
One of the major significance of the interface in the OOPs concept is to implement multiple inheritance. It means that a class can implement multiple interfaces.
Let’s see this in action:
public interface IEmployee
{
double TaxAmount { get; }
double CalculateCTC(int empId);
void DisplayTaxAmount(int empId);
}
public interface IProject
{
void ProjectExpenditure();
}
Here, we have 2 interfaces: IEmployee
and IProject
.
Now, we will create a class Employee
and implement both the interfaces:
public class Employee : IEmployee, IProject
{
public double TaxAmount => CalculateCTC(1)*5/100;
public void DisplayTaxAmount(int empId)
{
Console.WriteLine($"Tax Amount: {TaxAmount}");
}
public double CalculateCTC(int empId)
{
return 500000;
}
public void ProjectExpenditure()
{
throw new NotImplementedException();
}
}
Difference between Abstract and Interface
Now, Let’s summarize and see Abstract Class vs Interface.
- Abstract classes can have implementation for some of its members, but an interface can not have implementation for any members.
- Interface members are by default public and no explicit access modifier is required, but abstract class members can have access modifiers.
- An interface can be inherited from another interface only and not from an abstract class, but an abstract class can be inherited from another abstract class or an interface.
- Interface can not contain constructors, but the Abstract class may have a constructor.
- Abstract class allows single inheritance but Interface allows multiple inheritance.
- In general, we use the Abstract class as a base class.
- Abstract Class may contain static members, but an Interface does not contain static members.
- The interface allows only the public as an access modifier. An Abstract Class may contain public, private and protected members.
Use of Abstract Class and Interface in C#
Now, we understand Abstract Class and Interface. We also know how to implement these 2 in the C# application.
Let’s move to the next level of thinking.
Questions: Where can we use the interface and in which case will the Abstract Class fit?
Answer: Answer to this question:
Use of Abstract Class | Use of Interface |
1. For partial implementation, we can choose an Abstract class over an interface. 2. We can use the Abstract class if we share a common process across the child or sub-classes. | 1. A class can implement multiple interfaces. So, in case of multiple inheritance Interface is the only option. 2. For full implementation, we pick Interface. |
Summary
So, in this article, we learnt the difference between Abstract Class and Interface. We also learnt what are the uses of these two entities in C# and OOPs concept.
Keep Following: SharePointCafe.NET