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.
- 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.
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.
- We cannot create an object of Abstract Class but we can create a reference of it.
- 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.
- An abstract class can never be sealed or static.
- An abstract class can have abstract as well as non abstract methods.
- The abstract keyword can be used with class, methods, properties, indexers and events.
- Abstract members can only be declared inside an abstract class.
- An abstract member cannot be static or private.
- An abstract method cannot be marked virtual.
- A concrete class cannot inherit more than one abstract class, in other words multiple Inheritance is not possible.
- 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.
- 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.
- An interface is nothing more than a collection of semantically related abstract members.
- The exact number of members defined by a given interface always depends on the exact behavior one is attempting to model.
- An interface specification is similar to a signature in algebra.
- Interfaces never define data types.
- Interfaces never provide a default implementation of methods.
- Every member of an interface is automatically abstract (methods are virtual abstract).
- 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++).
- 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,