Search My Warehouse

2009-11-06

Abstract Class vs Interface with example

Abstract Class

-An abstract class is a special kind of class that cannot be instantiated.
-An abstract class is only to be sub-classed (inherited from).
-It enforces certain hierarchies for all the subclasses.
- A class may inherit only one abstract class, but may implement multiple number of Interfaces.


Interface

-An interface is not a class
-Interface have only Signature.
-
It does not have Contructor,destructor,Fileds
-


Difference:

*A class may inherit only one abstract class, but may implement multiple number of Interfaces. Say a class named Car needs to inherit some basic features of a vehicle, it may inherit from an Aabstract class named Vehicle. A car may be of any kind, it may be a vintage car, a sedan, a coupe, or a racing car. For these kind of requirements, say a car needs to have only two seats (means it is a coupe), then the class Car needs to implement a member field from an interface, that we make, say ICoupe.

*Members of an abstract class may have any access modifier, but members of an interface are public by default, and cant have any other access modifier.

*Abstract class methods may OR may not have an implementation, while methods in an Interface only have a definition, no implementation.

*Interface is used to "implements"; whereas abstract class is used to "extends".

* Interface can "extends" another interface, abstract class can "extends" another class and "implements" multiple interfaces.

*************************************************************************************

Multiple inheritance:
---------------------

Abstract class- A class may inherit only one abstract class.

Interface - A class may inherit several interfaces.

Access Modfiers
--------------------

An abstract class can contain access modifiers for the subs, functions, properties

An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public

Speed
-------


Abstract class- Fast

Interface -Requires more time to find the actual method in the corresponding classes.

************************************************************************************


Example:

Abstract Class

public abstract class Employee
{
//we can have fields and properties


//in the Abstract class

protected String id;
protected String lname;
protected String fname;

//properties


public abstract String ID
{
get;
set;
}

public abstract String FirstName
{
get;
set;
}

public abstract String LastName
{
get;
set;
}
//completed methods


public String Update()
{
return "Employee " + id + " " +
lname + " " + fname +
" updated";
}
//completed methods


public String Add()
{
return "Employee " + id + " " +
lname + " " + fname +
" added";
}
//completed methods


public String Delete()
{
return "Employee " + id + " " +
lname + " " + fname +
" deleted";
}
//completed methods


public String Search()
{
return "Employee " + id + " " +
lname + " " + fname +
" found";
}

//abstract method that is different


//from Fulltime and Contractor

//therefore i keep it uncompleted and

//let each implementation

//complete it the way they calculate the wage.


public abstract String CalculateWage();

}

------------------

Interface

public interface IEmployee
{

// protected String id;
// protected String lname;
// protected String fname;


//just signature of the properties

//and methods.

//setting a rule or contract to be

//followed by implementations.


String ID
{
get;
set;
}

String FirstName
{
get;
set;
}

String LastName
{
get;
set;
}

// cannot have implementation


// cannot have modifiers public

// etc all are assumed public

// cannot have virtual


String Update();

String Add();

String Delete();

String Search();

String CalculateWage();
}


----------------

we cannot call abstract class directly, so create a child class to inherit the abstract class:

Here,

Emp_Fulltime- Child Class.

Employee- Abstract Class.



public class Emp_Fulltime : Employee
{

public Emp_Fulltime()
{
}


public override String ID
{
get

{
return id;
}
set
{
id = value;
}
}

public override String FirstName
{
get

{
return fname;
}
set
{
fname = value;
}
}

public override String LastName
{
get

{
return lname;
}
set
{
lname = value;
}
}

//common methods that are

//implemented in the abstract class

public new String Add()
{
return base.Add();
}
//common methods that are implemented


//in the abstract class

public new String Delete()
{
return base.Delete();
}
//common methods that are implemented


//in the abstract class

public new String Search()
{
return base.Search();
}
//common methods that are implemented


//in the abstract class

public new String Update()
{
return base.Update();
}

//abstract method that is different


//from Fulltime and Contractor

//therefore I override it here.

public override String CalculateWage()
{
return "Full time employee " +
base.fname + " is calculated " +
"using the Abstract class...";
}
}

------------------------

class - Emp_fulltime2

Interface - IEmployee



public class Emp_fulltime2 : IEmployee
{
//All the properties and


//fields are defined here!

protected String id;
protected String lname;
protected String fname;

public Emp_fulltime2()
{
//


// TODO: Add constructor logic here

//

}

public String ID
{
get

{
return id;
}
set
{
id = value;
}
}

public String FirstName
{
get
{
return fname;
}
set

{
fname = value;
}
}

public String LastName
{
get
{
return lname;
}
set
{
lname = value;
}
}

//all the manipulations including Add,Delete,


//Search, Update, Calculate are done

//within the object as there are not

//implementation in the Interface entity.

public String Add()
{
return "Fulltime Employee " +
fname + " added.";
}

public String Delete()
{
return "Fulltime Employee " +
fname + " deleted.";
}

public String Search()
{
return "Fulltime Employee " +
fname + " searched.";
}

public String Update()
{
return "Fulltime Employee " +
fname + " updated.";
}

//if you change to Calculatewage().


//Just small 'w' it will raise

//error as in interface

//it is CalculateWage() with capital 'W'.

public String CalculateWage()
{
return "Full time employee " +
fname + " caluculated using " +
"Interface.";
}
}

------------------------------



public void Call_Interface()

private void InterfaceExample_Click(object sender,
System.EventArgs e)
{
try

{

IEmployee emp;

Emp_fulltime2 emp1 = new Emp_fulltime2();

emp = emp1;
emp.ID = "2234";
emp.FirstName= "Rahman" ;
emp.LastName = "Mahmoodi" ;
//call add method od the object


MessageBox.Show(emp.Add().ToString());

//call the CalculateWage method

MessageBox.Show(emp.CalculateWage().ToString());


}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

}

--------------------

public void call_AbstractClass()

{
Employee emp;

emp = new Emp_Fulltime();


emp.ID = "2244";
emp.FirstName= "Maria" ;
emp.LastName = "Robinlius" ;
MessageBox.Show(emp.Add().ToString());

//call the CalculateWage method


MessageBox.Show(emp.CalculateWage().ToString());

}


Source:

Codeproject: http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx

DotnetSpider: http://www.dotnetspider.com/resources/20897-What-Difference-Between-Interface-abstract.aspx

No comments:

Feed