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

Interface with real life Examples

Interfaces with real life examples An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class). For example, say we have a car class and a tricyclic class and a truck class. Each of these three classes should have a start engine() action. How the "engine is started" for each vehicle is left to each particular class, but the fact that  they must  have a start engine action is the domain of the interface. The syntax of an Interface We can implement single interface or multiple interfaces to the class. By default interfaces are public. We declare interface by using "interface" keyword. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CsharpnaijaInterfaceTutorial {     interface IEngine     {         void Start();     }...

The Out Parameter with Example in C#

  The Out Parameter with Example in C# In C#,  out  keyword is used to pass arguments to the method as a  reference type . The  out  keyword same as the  ref  keyword but the only difference is  out  doesn’t require a variable to be initialized before we pass it as an argument to the method but the  variable  must be initialized in called method before it returns a value to the calling method. The out parameter in C# is also useful to return more than one value from methods. Declaration of C# Out Parameter Following is a simple example of using  out  parameters in C#.   int x; // No need to initialize variable OutParameterMultTest ( out x ) ;   From the above declaration, we just declared a variable  x  and pass it to the method by using  out  parameter without assigning any value but as discussed, the variable must be initialized in called method before it returns a value...