Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Abstraction is an important part of Object Oriented Programming. Abstract classes help achieve abstraction by making only necessary information visible to the user while the rest is kept hidden.
You can also achieve abstraction using interfaces that only declare functions with empty bodies, and you need to define these functions when inherited by another class.
In this article, we will learn about abstract class and interface in C#, their pros, and cons, and finally, the difference between abstract class and interface in C#.
Abstract class Vs Interface in C#
In C#, an abstract class is a special class that cannot be instantiated. Therefore they are either partially executed or not at all. Subclasses can inherit an abstract class. It consists of constructors, instance variables, field events, and non-abstract methods and serves as a base class. An abstract class cannot be instantiated directly, whereas the members can be inherited by other classes and provide their own implementation.
On the other hand, an interfacedetermines the functionality that is impediment by a class, it consists of properties, events, and methods, but the execution depends upon a derived class. In an interface, the implementation part does not take place, and the methods are declared that can be implemented in the derived classes.
What is Abstract Class?
A class that we cannot instantiate is said to be an abstract class; that is, you can’t use this class to create objects.
Once you derive a class from an abstract class, you can implement the abstract definitions using the override keyword. An abstract class should have at least one abstract method.
We use the keyword abstract for declaring an abstract class.
Example:
C#
C#
using System;
public abstract class Shape { // Abstract method public abstract double Area(); }
// Create a subclass that inherits from the abstract class public class Circle : Shape { private double radius;
public Circle(double radius) { this.radius = radius; }
// Implement the abstract method public override double Area() { return Math.PI * radius * radius; } }
class Program { static void Main(string[] args) { // Create an instance of the Circle class Circle circle = new Circle(5.0);
// Call the Area method of the Circle class double area = circle.Area();
Console.WriteLine("Area of the circle: " + area); } }
Output
Explanation
In the above example, We defined an abstract class Shape with an abstract method Area(). Abstract methods are declared with the abstract keyword and have no implementation in the abstract class. We created a concrete subclass Circle that inherits from Shape. This subclass provides an implementation of the Area() method specific to circles. In the Main method, we created an instance of the Circle class and calculated the area of a circle with a given radius.
What is Interface in C#?
Interface in C# is just like an abstract class. It is like a blueprint of a class, and one cannot instantiate or create objects from an interface. The major difference between abstract class and interface in C# is that interfaces allow multiple inheritance.
Example:
C#
C#
using System;
// Define an interface public interface IShape { // Method signature for calculating area double Area(); }
// Create a class that implements the interface public class Circle : IShape { private double radius;
public Circle(double radius) { this.radius = radius; }
// Implement the Area method from the interface public double Area() { return Math.PI * radius * radius; } }
class Program { static void Main(string[] args) { // Create an instance of the Circle class Circle circle = new Circle(5.0);
// Call the Area method of the Circle class double area = circle.Area();
Console.WriteLine("Area of the circle: " + area); } }
Output
Explanation
In the above code snippet, we defined an interface IShape with a method signature Area(). We created a class Circle that implements the IShape interface. The class provides an implementation of the Area() method specified in the interface. In the Main method, we created an instance of the Circle class and calculated the area of a circle with a given radius.
Difference between Abstract Class and Interface in C#
Parameters
Abstract Class
Interface
Method Implementation
Can have abstract and non-abstract methods.
Can have only abstract methods.
Multiple Inheritance
Does not support multiple inheritance.
Supports multiple inheritance.
Static Members
Can have static members.
Cannot have static members.
Constructor
Can have a constructor.
Cannot have a constructor.
Performance
Generally faster than interfaces in terms of performance.
Generally slower than abstract classes in terms of performance.
Implementation Completeness
Can be fully, partially, or not implemented.
Must be fully implemented.
Keyword for Declaration
Uses the "abstract" keyword for declaration.
Uses the "interface" keyword for declaration.
Access Modifiers
Supports various access modifiers like public, protected, private, etc.
By default, all members are public.
Class Usage
A class can inherit from only one abstract class.
A class can implement multiple interfaces.
Core vs. Peripheral Abilities
Implements the core identity of a class.
Implements the peripheral abilities of a class.
Implementing Keyword
Uses the "extends" keyword for implementation.
Uses the "implements" keyword for implementation.
Advantages of Abstract Class
It helps to write shorter codes.
It helps to avoid code redundancy by avoiding code duplication.
It allows code reusability.
It is faster than an interface.
Abstract classes can inherit from a class and interfaces.
Disadvantages of Abstract Class
We cannot instantiate abstract classes.
It does not support multiple inheritance.
Advantages of Interface
It allows multiple inheritance, unlike abstract classes.
It enables parallel application development.
You can implement loosely coupled systems using interfaces.
It allows mocking, which improves unit testing.
It helps to implement inversion of control or dependency injection.
Disadvantages of Interface
An interface is slower in performance because it needs more time while calling a method than through a class type.
If you add new members to an interface, then you need to implement those members in all the classes implementing the interface.
Let’s see the difference between abstract class and interface in C# in the next section.
Q. Why abstract class is faster than interface in C#?
An abstract class can provide method implementations, reducing the need for method lookups and making it potentially faster. Interfaces, being contract-only, may require additional lookups for method implementation.
Q. Can we create a constructor in the abstract class?
Yes, you can create a constructor in an abstract class in C#. It allows you to initialize common properties or perform setup tasks when a subclass is instantiated.
Q. Can we create an object of interface in C#?
No, you cannot create an object of an interface in C#. Interfaces define a contract for methods but do not provide an implementation, so they cannot be instantiated directly.
Q. Which is better, abstract class or interface?
Choose interfaces over abstract classes. Interfaces separate design from implementation, ensuring shared method behaviour, even for purely abstract classes requiring inheritance.
Q. When to use abstract class and interface in C#?
An abstract class is used when a user wants to create functionality that can be implemented in the subclasses, while an interface is used when we only want to define specific methods that a class can implement.
Conclusion
In this article, we learned about the difference between abstract class and interface in C#.
We hope that this blog has helped you enhance your knowledge regarding the difference between abstract class and interface in c#.