Thursday, April 26, 2007

Interface and abstract feature in C#

In this I tryed to explain the Concept of Interface and abstract feature of C# with Examples


What's an Interface?
An interface is a reference type object with no implementation.


Why Use Interfaces?
To make our code more reusable, we can provide interfaces to our implementation. This ONLY works if we are disciplined about not altering interfaces after they have been deployed (that would cause the same cascading breaks that non-interface development does). We can always add functionality to an interface without breaking things, but if we alter the existing "hooks" into our implementation we loose one of the primary benefits of using the interface. This is the downside of interface based development. If interfaces are poorly designed and have to change or not easy to implement we will not receive the benefits of using them and all they achieve is to make our code more complex. We need to think out the interfaces very carefully at design time before getting too far into the implementation because once they are implemented and made available to other resources they should not change.

Here's a concrete example which Explains how to use Interface, Consider you want to become as Me!! , so what you need to do,…Do all the things which I am doing ..So I will declare what all the things which I will be doing (ItoBecomeasMe ) . And my follower who wants to become as me should Inherit me.


using System;

namespace Test
{
///
/// Summary description for testInterface.
///

public class testInterface
{
public testInterface()
{
//
// TODO: Add constructor logic here
//
}





// Interface I am declaring methods which I am did, If you want to become as me do those methods
public interface IToBecomeasMe
{

// I am declaring Methods which you should have to become as me
string myname{ get; set ;}
void DoBE () ;
bool IsPassed{get;}
void doCoding();

}






// My follower has to do all the things which I am doing
// so this class has to Implement all the methods of Base class

public class MyFollower : IToBecomeasMe
{

private bool test_IsPassed ;
private string name ;
private string mba_Degree ;

public string myname
{
get
{
return name ;

}
set
{
name = value ;
}
}

public void DoBE()
{
Console.Write("I must do BE") ;
}

// Compiler will Identify methods with method name and parameter
//type so it treat as this method diff than the above considering parameter type

public void DoBE( bool Fail)
{
Console.Write("Failed") ;
}
public bool IsPassed
{
get
{

return test_IsPassed ;
}
}


public void doCoding()
{
Console.Write("I must do Coding") ;
}


// This method is one which was not In Base class, the thing is You //must have all the Methods of Base class with your owned class
public void DoMBA()
{
Console.Write("I did BE and also MBA") ;
}

}





// My special follwer

//Abstract classes are one of the essential behaviors provided by .NET. 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'.
//An abstract class means that, no object of this class can be instantiated, but can //make derivations of this.

public abstract class MySpecialFollower : IToBecomeasMe
{


private bool test_IsPassed ;
private string name ;
private string mba_Degree ;


// Declare abstract class which force the derived class to override this method which make him special
public abstract int goToUS(int year) ;


// Implementing the other methods
public string myname
{
get
{
return name ;

}
set
{
name = value ;
}
}

public void DoBE()
{
Console.Write("I must do BE") ;
}

public bool IsPassed
{
get
{

return test_IsPassed ;
}
}


public void doCoding()
{
Console.Write("I must do Coding") ;
}
// This method is one which was not In Base class, the thing is You //must have all the Methods of Base class with your owned class

public void DoMBA()
{
Console.Write("I did BE and also MBA") ;
}

}
}




// In this method I will create a derived class which inherits all the methods of Special Follower .

public class FollwerofSpecialPerson : testInterface.MySpecialFollower
{


// this will force to user to add method goToUS(year) , otherwise throws error
//Also, note that an abstract class does not mean that it should contain abstract members. //Even we can have an abstract class only with non abstract members.


public override int goToUS(int year)
{
int money ;
// earn lots of money
for ( money = 0 ; money <= 10000 ; money++ )
{

money = money * year ;
}

return money ;
}


// another method with diff return type who settel in US
public bool goToUS(bool settel)
{
int money ;
// earn lots of money
for ( money = 0 ; money <= 10000 ; money++ )
{

money = money * money ;
}

return true ;
}

}
}



static void Main (string[] args)
{

//this throws error becoes we cant create Instance of abstract class
// testInterface.IToBecomeasMe me = new testInterface.MySpecialFollower
();
testInterface.IToBecomeasMe me = new testInterface.MyFollower();
me.myname = "BABU" ;
me.DoBE() ;
if (me.IsPassed)
{
me.doCoding() ;
}
}
}
}

Note :

An abstract class cannot be a sealed class. I.e. the following declaration is
incorrect.


abstract sealed class absClass
{
}
Declaration of abstract methods are only allowed in abstract
classes.
An abstract method cannot be private

The access modifier of the abstract method should be same in both
the abstract class and its derived class. If you declare an abstract method as
protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.

Diff. between Interface and abstract

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.