Sunday, February 3, 2013

Interface and Abstract Classes



What is an Interface? , What is an abstract class?
Difference between Interface and abstract class
When should we go for Interface and abstract class?

Hi Folks,

                   It’s sometimes very tough to decide when to use interface and abstract class. It may be software architecture or a developer who is involved in designing software architecture. I have gone through many interviews during my college days where I used to just remember what the basic different between both of them was!  But I was not aware of from where exactly some one needs to use interface and abstract classes while designing a software architecture.

Today I thought to help all those fresher of IT field to know what the Interface and abstract class means.

When should we use what? A fresher always worries about the programming languages. Most of them think that interface and abstract class may differ while implementing in different programming languages. Concept of interface and abstract class remain the same for all programming languages.

Note: I have used .net framework and C# as programming language.

Abstract Class: Abstract classes are one of the essential behaviors provided by object oriented programming languages. Commonly, you would like to make classes that only represent base classes, and don’t want anyone to create objects of these class types. You can make use of abstract classes to implement such functionality in C# using the modifier 'abstract'.
What Abstract Class can contain:
ð     Abstract class can have Fields
       eg: protected sting _strName;
ð     Abstract class can have properties
       eg: public string Name{get; set;}
ð     Abstract class can have methods
ð     Abstract class can have Constructor and destructor
ð     Abstract class can have Static methods
ð     Abstract class can have Indexer
ð     Abstract class can have either abstract method or not abstract methods
ð     Abstract members do not have any implementation in the abstract class, but the same has to be provided in its derived class.
ð     Non abstract methods should have body in abstract class
ð     Methods in abstract class should only public or protected
ð     If we declare a method without an abstract keyword in abstract class; it shows an error and ask for the body of the method to be written in abstract class or either declare it as abstract method.
ð     If we declare an abstract method in abstract class it should be implemented in derived class otherwise program will display an error message while compiling.

What abstract class can’t have?
ð     Method in abstract class can not have private member
ð     Abstract method in base abstract class should not have body.
ð   Abstract methods can not be marked as Virtual because all abstract methods are by default virtual.
ð  Abstract method can not be static.
      ð  Abstract class can not be Static, Private, Sealed and Internal. Abstract class is by default public.


Interfaces:
Interface provides a way to achieve runtime polymorphism. Using interface we can invoke functions from different class

What interface contain:
ð     An interface can have methods signature
ð     Interface can have Delegate signature
ð     Interface can have Event signature
ð     Interface can have Properties signature.
ð     So a interface should look like below:

public interface Student
    {
        string FirstName
        {
            get;
            set;
        }
        void MyMessage();
        event EventHandler MyTest;
    }
What Interface can not have?
ð     Interface can not have constructor
ð     Interface can not have destructor
ð     Interface can not have static methods
ð     Interface can not have fields
ð     Interface methods can not have public or private accessibility modifier by default every thing is public inside interface.

When to use Abstract class:
ð     For instance, I am developing a solution for a college which has different branches and departments. All the branches and departments are looked over by the University. If any notice is issued by the University; it should be circulated to all the branches and departments. The notice may comprise of changes in fee structure or some departmental specific information.

Assume I have already a solution with following architecture:
public abstract class VTUUniversity
    {
        public string _strCollegeName;
        public string Name
        {
            get;
            set;
        }
       protected abstract void FeeStructure();
  
    }

  public class MVJCollege : VTUUniversity
    {
      
        protected override void FeeStructure()
        {
            Console.WriteLine("I am in MVJ College");
        }
    }

        public class RamayanCollege : VTUUniversity
       {
         protected override void FeeStructure()
         {
            Console.WriteLine(" I m in RamayaCollege");
         }
       }
ð     There are two colleges affiliated and branches of VTU University. Now suppose University does some changes in examination fee structure of MCA course and wants to inform all the branches to implement the fee structure; but on the contrary , Ramaya College does not have MCA course so they don’t need to implement or publish this notice on their board.

ð     So the new structure will look like this:

public abstract class VTUUniversity
    {
        public string _strCollegeName;
        public string Name
        {
            get;
            set;
        }
       protected abstract void FeeStructure();
       public virtual void MCADeptFeeStructure()
       {
        Console.WriteLine("MCA Depatment Has Increased Examination fee by: 2000");
        Console.ReadLine();
       }
    }

public class MVJCollege : VTUUniversity
    {
        protected override void FeeStructure()
        {
            Console.WriteLine("I am in MVJ College");
        }
        public override void MCADeptFeeStructure()
        {
            Console.WriteLine("I am in MVJ College Fee sructure");
            Console.ReadLine();
        }
    }

    public class RamayanCollege : VTUUniversity
    {
        protected override void FeeStructure()
        {
            Console.WriteLine(" I m in RamayaCollege");
        }
    }


  class Program
    {
        static void Main(string[] args)
        {
            MVJCollege objMvjc = new MVJCollege();
            RamayanCollege objRamaya = new RamayanCollege();
            objMvjc.MCADeptFeeStructure();
        }

    }

ð     So what we noticed here is, we have added a non abstract method on abstract class with body and it does not reflect any changes in RamayanCollege  class because they don’t have any related course and not need to implement this method. While MVJCollege class has implemented and did there customization on derived class.
ð     This says that we should use abstract class as a base class when we are developing a large application which is widely used by many clients. So if any changes are made in base class does not affect the existing client functionality.

When To Use Interface:

ð     In case of the situation mentioned above, if we would like to use a base class as in interface then we would need to implement MCADeptFeeStructure interface in all the branches of University. It does not matter weather the college has MCA course or it does not!

ð     Interface should be used if we are sure that there will not be any further changes in the base class in feature.
ð     If we want to implement multiple base class because we can inherit multiple base class if we use interface while this is not possible in case of abstract class.

Recommendation:
v      If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
v      If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
v      If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
v      If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

Best Wishes..
Happy Coding..