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

Classes in C# Explained

C# Class Explained A class is nothing but an encapsulation of properties and methods that are used to represent a real-time entity, as explained by Guru99 . For instance, if you want to work with Guest’s data as in our previous DataDriven Web application . The properties of the Guest would be the Id, GuestName, Address, Phone number etc of the Guest. The methods would include the entry and modification of Guest data. All of these operations can be represented as a class in C# as shown below. using System; namespace CsharpnaijaClassTutorial {     public class Guest     {         public int Id { get ; set ; }         public string GuestName { get ; set ; }         public string Address { get ; set ; }         public string WhomToSee { get ; set ; }     ...

ASP.NET MVC Views

Views in ASP.NET MVC Application explained Find a related article By  Steve Smith  and  Luke Latham from Microsoft Corporation here In the Model-View-Controller (MVC) pattern, the  view  handles the application's data presentation and user interaction. A view is an HTML template with embedded  Razor markup . Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client. In ASP.NET MVC, views are  .cshtml  files that use the  C# programming language  in Razor markup. Usually, view files are grouped into folders named for each of the application's  controllers . The folders are stored in a  Views  folder at the root of the application as shown: The  Home  controller is represented by a  Home  folder inside the  Views  folder.  The  Home  folder contains the views for the  About ,  Contact , and  Index...

ASP.NET MVC Routing

ASP.NET MVC Routing ASP.NET MVC routing is a pattern matching system that is responsible for mapping incoming browser requests to specified MVC controller actions. When the ASP.NET MVC application launches then the application registers one or more patterns with the framework's route table to tell the routing engine what to do with any requests that matches those patterns. When the routing engine receives a request at runtime, it matches that request's URL against the URL patterns registered with it and gives the response according to a pattern match. Routing pattern is as follows A URL is requested from a browser, the URL is parsed (that is, break into controller and action), the parsed URL is compared to registered route pattern in the framework’s route table, if a route is found, its process and send response to the browser with the required response, otherwise, the HTTP 404 error is send to the browser. Route Properties ASP.NET MVC routes are res...