Adnan Mohsin December 26, 2016 Comments 2.00

"What is the difference between an Abstract Class and an Interface


One question is always out there in most interviews. What is the difference between an Abstract Class and an Interface and why do you use one over the other? It seems like a simple question but most often, the answer to this question is not sufficient for the interviewer and can cost a job. So, I have decided to write an article about them and have tried to explain it in as simple a manner as I can. 

These are the most significant concepts in programming and without them we cannot achieve better code. If you haven't prepared them good, it can cost you a job. So, these concepts are not impossible to learn but their role in an interview is quite crucial. Simple approach: "What, Why and How".
  • What: What is it?
  • Why: Why do I need to learn it?
  • How: How can I work with it?

Firstly we talk about Abstract class.

Abstract Class
What is an Abstract Class? 

The meaning of the word "abstract" is a thought or idea that has no physical existence and is just conceptual accorind to the dictionary. We can use the idea to build something of a physical existence. In the MSDN Library, the "abstract" keyword indicates that the thing has a missing or incomplete implementation and must be completed by others. 

In programming the abstract keyword can be used with classes, methods, properties, indexers and events also. If we use the abstract keyword with a class, it indicates that the class is to be a base class and can have abstract methods that must be implemented in a sub class (physical existence).

An abstract cannot be instantiated. Its implementation logic is provided by the classes that derive from it. It can have both abstract and non-abstract methods.It is not compulsory to have only abstract methods in an abstract class. We can also have an abstract class with only non-abstract methods.

Why do we need an Abstract Class?

With an Abstract Class, we can provide some kind of default functionality for all derived classes to inherit from. This is useful to avoid code duplication and redundancy in many cases. 

Suppose we are defining an abstract class as Vehicle and then inherit it to Car and MotorBike subclasses. PractiAcseleratey we don't want an object of an Vehicle class since we first need to know the model of Vehicle. So, the Vehicle class should be an abstract class that contains some predefined functions like Acselerate() and Break() for all Vehicle models to share . We can also add abstract methods like Model() and Color() into the Vehicle class that must be implemented by all the subclasses inheriting Vehicle. The main advantage of this approach is, whenever we inherit the Vehicle class into a derived class, say Marcedes, we need not define the Acselerate() and Break() methods again. We just need to implement the abstract methods and enough good to go. It helps to provide default functionality in all the derived classes and also avoids code duplication. 

Abstract classes are also useful in the case of modification to the project. If we plan on updating the base class in project, it is better to make the class abstract. Because we can define a functionality in an abstract base class and automatiAcseleratey all the inheriting classes will have the same functionality without changing the hierarchy.

Key Points

  1. We cannot create an object of Abstract Class but we can create a reference of it.
  2. An inheritance between abstract to abstract classes is possible. We don't need to implement abstract methods of the base abstract class into a derived abstract class. We can implement it later in concrete classes. 
  3. An abstract class can never be sealed or static.
  4. An abstract class can have abstract as well as non abstract methods.
  5. The abstract keyword can be used with class, methods, properties, indexers and events.
  6. Abstract members can only be declared inside an abstract class.
  7. An abstract member cannot be static or private. 
  8. An abstract method cannot be marked virtual. 
  9. A concrete class cannot inherit more than one abstract class, in other words multiple Inheritance is not possible.
  10. Without an abstract class, we cannot implement the Template Method Pattern.


What an interface is?

In the real world, an interface means a medium to interact with something. To be precise, it's a point where two systems, subjects, organizations and so on meet and interact. There are a few rules for the interaction to be done. Suppose you are going for an interview of Programmer Profile. The interview is only possible if the interviewer and you speak the same language. Moreover, you and the interviewer have the same skill set of programming languages to discuss upon.

Similarly, in the programming world, an interface means a contract to interact with multiple code modules. If a class wants to communicate with an interface, it must implement it and define its members. Consider it like the interviewer's question and you need to answer it correctly, if you want the job.

The MSDN Library defines the interface like a pure abstract class. An interface contains only the signatures of methods, properties, events, or indexers. It has no implementation of its own and can only be implemented by a class or a struct. Any of the two that implement the interface must provide the definitions to members specified in the interface. It is like a contract for all the derived classes to follow.

An interface is declared using the interface keyword. interface members are implicitly public and abstract, so we cannot prefix any access modifiers to it. An interface cannot contain fields, constant members, constructors, destructors and static members.

Why do we need an interface

An interface is not a class. It contains only method signatures. It has no implementation on its own and cannot be instantiated. Its implementation logic is provided by the classes that derived from it. An interface is mostly considered to be a pure abstract class. However, there is the advantage of using an interface over an abstract class; that is "Multiple Inheritance Support". In C#, two classes (either abstract or concrete) cannot be inherited by the same derived class. It causes ambiguity in the derived class if both have the same method signature. We can do multiple inheritance in C# using interfaces.

An interface plays a vital role in the Service Oriented Architecture (SOA). In WCF, we use interfaces to define Service Contracts. A single class can implement any number of Service Contract Interfaces. It is generally accepted as the best practice to do so. However, we can also use classes for Service Contracts.

Most of the Design Patterns and Principles are based on interfaces rather than class inheritance. Some of the examples are Builder Design Pattern, Factory Pattern, Interface Segregation Principle and so on.

Key Point

  1. An interface has no implementation and cannot be instantiated. However, it can be referenced to the class object that implements it. It may be noted that the object can only access the inherited members of the interface.
  2. An interface is nothing more than a collection of semantically related abstract members.
  3. The exact number of members defined by a given interface always depends on the exact behavior one is attempting to model.
  4. An interface specification is similar to a signature in algebra.
  5. Interfaces never define data types.
  6.  Interfaces never provide a default implementation of methods.
  7. Every member of an interface is automatically abstract (methods are virtual abstract).
  8. C# only supports single inheritance, the interface-based protocol allows a given type to support numerousbehaviors, while avoiding the issues that arise when deriving from multiple base classes (as in C++).
  9. In C# it is completely permissible to create an interface that derives from multiple base interfaces (unlike Abstract Class).

What is the difference between Interface and Abstract Class?

Theoretically there are some differences between Abstract Class and Interface which are listed below:

  • A class can  implement any number of  interfaces but a subclass can at most use only one abstract class.
  • An  abstract  class  can  have  non-abstract  methods  (concrete  methods)  while  in  case  of interface all the methods has to be abstract.
  • An abstract class can declare or use any variables while an  interface  is not allowed  to do so.
  • In an abstract class all data member or functions are private by default while  in  interface all are public, we can’t change them manually.
  • In an abstract class we need to use abstract keyword to declare abstract methods while in an interface we don’t need to use that.
  • An  abstract  class  can’t  be  used  for multiple  inheritance while  interface  can  be  used  as multiple inheritance.
  • An  abstract  class  use  constructor  while  in  an  interface  we  don’t  have  any  type  of  constructor.




This article can help you to understand the various aspects of Abstract Classes. Your feedback and constructive opinion is always appreciated,

Search widget


Latest posts

Design Patterns Design Patterns in Programming

What is a Design Pattern? According to MSDN, "A design pattern is a description of a set of interacting classes that provide a framework for a solution to a generalized problem in a specific context or environment. In other words, a pattern suggests a solution to a particular problem or issue in object-oriented software development.”

May 05, 2017 16 0

C# Ref and Out parameters in C Sharp

This tutorial explains the concepts of Ref and Out Keywords in C # programming. These two keywords are also frequently asked in most interviews.

January 15, 2017 16 0

WPF Vector Graphics in WPF

In this article, I will discuss how we can create a Line, Rectangle, ellipse and Polygon control in WPF at design-time using XAML and at run-time using C#.I will also explain their properties to format such as fill property. After that, i will show you to set an image as the background of any shape object.

January 08, 2017 16 0

C# What is the difference between an Abstract Class and an Interface

One question is always out there in most interviews. What is the difference between an Abstract Class and an Interface and why do you use one over the other? It seems like a simple question but most often, the answer to this question is not sufficient for the interviewer and can cost a job. So, I have decided to write an article about them and have tried to explain it.

December 26, 2016 16 0