Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Medium

Difference between Abstract class and Interface in C#

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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.

Difference between Abstract class and Interface in C#

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 interface determines 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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

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

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.

Also Read About - singleton design pattern in c#

Frequently Asked Questions

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#.

Check out these useful blogs on - 

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available, interview puzzles, take a look at the interview experiences, and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow!

Happy Reading!!‍

Topics covered
1.
Introduction
2.
Abstract class Vs Interface in C#
3.
What is Abstract Class?
3.1.
C#
4.
What is Interface in C#?
4.1.
C#
5.
Difference between Abstract Class and Interface in C#
6.
Advantages of Abstract Class
7.
Disadvantages of Abstract Class
8.
Advantages of Interface
9.
Disadvantages of Interface
10.
Frequently Asked Questions
10.1.
Q. Why abstract class is faster than interface in C#?
10.2.
Q. Can we create a constructor in the abstract class?
10.3.
Q. Can we create an object of interface in C#?
10.4.
Q. Which is better, abstract class or interface?
10.5.
Q. When to use abstract class and interface in C#?
11.
Conclusion