Skip to main content

Differences between Abstract class and Interface in C#


Differences between Abstract class and Interface in C#


What is an Abstract Class?


An abstract class is a special type of class that cannot be instantiated. An abstract class is designed to be inherited by subclasses that either implement or override its methods. In other words, abstract classes are either partially implemented or not implemented at all. You can have functionality in your abstract class—the methods in an abstract class can be both abstract and concrete. An abstract class can have constructors—this is one major difference between an abstract class and an interface. You can take advantage of abstract classes to design components and specify some level of common functionality that must be implemented by derived classes.

Abstract class syntax
//Syntax of Abstract Class
     public abstract class AbstractClass
     {

     }

Sample Code that demonstrate the use of Abstract Class
// C# program to illustrate the
     // concept of abstract class

     // abstract class 'CSharp'
     public abstract class CSharp
     {

          // abstract method 'CSharpMethod()'
          public abstract void CSharpMethod();
     }

     // class 'CSharpNaija1' inherit
     // in child class 'CSharp'
     public class CSharpNaija1 : CSharp
     {

          // abstract method 'CSharpMethod()'
          // declare here with
          // 'override' keyword
          public override void CSharpMethod()
          {
              Console.WriteLine("Class name is CSharpNaija1");
          }
     }

     // class 'CSharp' inherit in
     // another child class 'CSharpNaija2'
     public class CSharpNaija2 : CSharp
     {

          // same as the previous class
          public override void CSharpMethod()
          {
              Console.WriteLine("Class name is CSharpNaija2");
          }
     }

     // Driver Class
     public class main_method
     {

          // Main Method
          public static void Main()
          {

              // 'obj' is object of class 'CSharp' class '
              // CSharp' cannot be instantiate
              CSharp obj;

              // instantiate class 'CSharpNaija1'
              obj = new CSharpNaija1();

              // call 'CSharpMethod()' of class 'CSharpNaija1'
              obj.CSharpMethod();

              // instantiate class 'CSharpNaija2'
              obj = new CSharpNaija2();

              // call 'CSharpMethod' of class 'CSharpNaija2'
              obj.CSharpMethod();
          }
     }

What is an Interface?


An interface is basically a contract—it doesn’t have any implementation. An interface can contain only method declarations; it cannot contain method definitions. Nor can you have any member data in an interface. Whereas an abstract class may contain method definitions, fields, and constructors, an interface may only have declarations of events, methods, and properties. Methods declared in an interface must be implemented by the classes that implement the interface. Note that a class can implement more than one interface but extend only one class. The class that implements the interface should implement all its members. Like an abstract class, an interface cannot be instantiated.

Interface syntax

//Interface Syntax
     public interface ICsharpnaijaInterface
     {
          void CSharpMethod();
     }

Sample Code that demonstrate the use of Abstract Class

// C# program to illustrate the
     // concept of interface
     //using System;

     // A simple interface
     interface interface1
     {

          // method having only declaration
          // not definition
          void show();
     }

     // A class that implements the interface.
     class MyClass : interface1
     {

          // providing the body part of function
          public void show()
          {
Console.WriteLine("Welcome to Csharp Naija!!!");
          }

          // Main Method
          public static void Main(String[] args)
          {

              // Creating object
              MyClass obj1 = new MyClass();

              // calling method
              obj1.show();
          }
     }

Difference between Interface and Abstract Class


Abstract Class
Interface
Multiple inheritance is not achieved by abstract class.
Multiple inheritance is achieved by interface.
It contains both declaration and definition part.
It contains only a declaration part.
It contain constructor.
It does not contain constructor.
It can contain static members.
It does not contain static members.
It can contain different types of access modifiers like public, private, protected etc.
It only contains public access modifier because everything in the interface is public.
The performance of an abstract class is fast.
The performance of interface is slow because it requires time to search actual method in the corresponding class.
It is used to implement the core identity of class.
It is used to implement peripheral abilities of class.
A class can only use one abstract class.
A class can use multiple interface.
If many implementations are of the same kind and use common behavior, then it is superior to use abstract class.
If many implementations only share methods, then it is superior to use Interface.
Abstract class can contain methods, fields, constants, etc.
Interface can only contain methods.
It can be fully, partially or not implemented.
It should be fully implemented.

These both C# Interface vs Abstract Class are great object-oriented programming concepts that are used highly in developing applications as per the requirement. It is purely selected the by the technical leads with which they are more comfortable and the business requirement. These both C# Interface vs Abstract Class are easy to use and simple to learn in any programming language.

When to Use Interface or Abstract Class in our application


In C#, an Abstract class or interface are been used for data abstraction. An interface is better than abstract class when multiple classes need to implement the interface. The member of the interface cannot be static. The only complete member of an abstract class can be static.
C# does not support multiple inheritances, interfaces are mainly used to implement the multiple inheritances. As a class can implement more than one interface and only inherit from one abstract class. An interface is mainly used only when we do not require the implementation of methods or functionalities. An abstract class is used when we do require at least default implementation.


Points to Remember

1.  Interface and Abstract classes cannot be instantiated
2.  A Class can inherit more than one interface but only one abstract class
3.  Interface cannot have member implementation but abstract may have one or more.

You can read more about abstract class and interface here

Comments

Popular posts from this blog

Collections in C#

Collections in C# In our previous article , we have learned about how we can use arrays in C#. Arrays in programming are used to group a set of related objects. So one could create an array or a set of Integers, which could be accessed via one variable name. What is Collections in C#? Collections are similar to Arrays, it provides a more flexible way of working with a group of objects. In arrays, you would have noticed that you need to define the number of elements in an array beforehand. This had to be done when the array was declared. But in a collection, you don't need to define the size of the collection beforehand. You can add elements or even remove elements from the collection at any point of time. This article will focus on how we can work with the different collections available in C#. There are three distinct collection types in C#: standard generic concurrent The standard collections are found under the System.Collections. They do not store elemen...

The String.Join Method in C# Explained

The String.Join Method in C#   The string.Join concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. Overloads of string.Join Method Description Join(Char, Object[]) Concatenates the string representations of an array of objects, using the specified separator between each member. Join(Char, String[]) Concatenates an array of strings, using the specified separator between each member. Join(String, IEnumerable<String>) Concatenates the members of a constructed IEnumerable<T> collection of type String, using the specified separator between each member. Join(String, Object[]) Concatenates the elements of an object array, using the specified separator between each element. Join(String, String[]) Concatenates all the elements of a string array, usi...

System.IO Namesapce in C#

  System.IO Namesapce in C# A  file  is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a  stream . The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the  input stream  and the  output stream . The  input stream  is used for reading data from file (read operation) and the  output stream  is used for writing into the file (write operation). From the above definition of file, the C# provides a namespace that enable us to manipulate file in C# called System.IO.   System.IO  is a  namespace  and it contains a standard IO (input/output) types such as classes , structures , enumerations , and  delegates  to perform a read/write operations on different sources like file, memory, network, etc.   System.IO Classes The table below shows differen...