Introduction
I think this is the most frequently question asked in interviews. This arcticles gives an idea about the usage of abstract classes and interfaces. What to use When?
Background
Before the option of interfaces abstract classes were used heavily for providing a well known structure to the client applications. Abstract classes let you define an specific behavior for client classes. Interfaces are majorly used term in Object Oriented Technologies. Interface based programming is also called Programming By Contract. What does it mean? We will discuss this in detail.
Using the code
To give you an idea that Abstract Classes are the classes which can not be instantiated. They are there for implementation. An abstract class can have abstract functions as well as concrete implementations too. A class is called abstract if it has at least one abstract function. Following is an example of an abstract class-
// // Abstract Class Example // public abstract class MyAbstractClass { //Class Implementation... //... //Abstract Method. This method should be implemented in derived class public abstract void AbstractMethod(int parameterName); }
Similarly Interfaces are also like the abstract classes which can not have implementation of functions. An interface would always have the signatures of the functions.
////// Example of Interface /// public interface IMyInterface { //Interface method Signatures... //It is important to mention here that an Interface can not have implementation of its members. //Secondly, all the interface members are by default public. They can not be written as by any other specifier void myTestInterfaceFunction(int parameterName); }
Interface programming is called the Programming by Contract because the class which is implementing the interface has to implement all the functions of interface. Interfaces is the only way provide multiple inheritence is C#. A class can not be inherited by more than one classes but it can implement more than one interfaces simultaneously. This was a short descritption about the Abstract Classes and Interfaces. Now the real thing comes in to picture. That What to Use When? So following are the points to remeber duirng the usage of Abstract Classes and Interfaces-
1. Interfaces should be used when creating a standalone project which can be changed at anytime, use an interface in preference to an abstract class; because, interfaces offers more design flexibility.
2. Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance.
3. Use an interface to design a polymorphic hierarchy for value types.
4. Use an interface when an immutable contract is really required.
5. Use an abstract class to define a common base class for a family of types.
6. Use an abstract class to provide default behavior to your application.
7. Subclass only a base class in a hierarchy to which the class logically belongs.
8. If you add a new functionality in abstract class then derived class can easily implement without breaking the versioning functionality. But in case of Interfaces addition of new interface member breaks the versioning functionality.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
About the Author
ANURAG VISHNOI | I am a Sr. Software Developer in a well known MNC Software Company SAP Labs, located in Gurgaon. Which is in National Capital Region of India. I have around 3 years of experience in C#.net technologies. I would like to read and write the articles related to Dot Net framework as well as Object Oriented Concepts.
|