Click here to Skip to main content
Licence CPOL
First Posted 30 Jan 2008
Views 15,151
Bookmarked 27 times

Abstract Classes Vs Interfaces

By | 30 Jan 2008 | Article
This articles gives you an idea about the usage of Abstract Classes and Interfaces. What to use When?

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.

		       /// <summary>
                       /// Example of Interface 
                       /// </summary>
                       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

Technical Lead
SAP Labs India Ltd., Gurgaon, Haryana, India
India India

Member

I am a Sr. Software Developer in a well known MNC Software Company SAP Labs, located in Bangalore. Which is in National Capital Region of India. I have around 5 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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionSignature Pad Control using c# and Framework 4.0 Pinmembermayuank kushwaha8:45 5 Aug '11  
GeneralMy vote of 3 Pinmemberonurag1923:01 16 Jul '11  
GeneralMy vote of 2 Pinmembersirama200419:32 31 Oct '10  
QuestionSuper Material but for whom..? PinmemberUdayakumar Venkataravanappa5:51 29 Sep '09  
QuestionHow did this show up as a "Featured article"? PinmemberPIEBALDconsult7:51 19 Feb '09  
GeneralWork Flow PinmemberAVT VISHNOI19:22 9 Jan '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 30 Jan 2008
Article Copyright 2008 by ANURAG VISHNOI
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid