Showing posts with label Differences between abstract and interface. Show all posts
Showing posts with label Differences between abstract and interface. Show all posts

Monday, May 9, 2011

Differecne Between Interfaces and Abstract ?

Differences between Interface and Abstract Class?
Interfaces :
--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
 ____________________________________
Public Interface ImyInterface

{
String Method1();
int Method2();

}
Public Class myClass :ImyInterface
{
string Method1()
{
//Code
}
int Method2()
{
//Code
}
}
____________________________________________________________________________
---Requires more time to find the actual method in the corresponding classes. (Slow )


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

  Abstract Class:
   These Classes Cannot be instantiated, 
 ---These are used if additional functionality is needed in derived classes,
 ---These Implemented partially or No Implementation
--Fast In Operation
 ____________________________________________________________
abstract class MyAbstractClass
{
public MyAbstractClass()
{
// Code to initialize the class goes here.
}

abstract public void Method1();
abstract public void Method2(int A);
abstract public long Method3(int B);
}
class MyClass : MyAbstractClass
{
public MyClass ()
{
// Initialization code goes here.
}

override public void Method1()
{
// code goes here.
}

override public void Method2(int A)
{
// code goes here.
}

override public long Method3(int B)
{
//code goes here.
}
}