Abstract Class and Interface

Question posted in Computer Software on 11 2009
Rate question difficulty level 2 Votes
What is the difference between Abstract class and Interface?
 
 
6 Answers
 
Abstract classes allow for default default function definition. This means that whatever class extends the abstract class will have access to this. If we have a base class where all the classes will perform the same function, then we can define that in our Abstract class. An interface is a list of functions or properties that if a class implements it, it will have to have those functions defined within it. It is a situation of “Is-A” vs “Can-Do-this”. Objects that extends an Abstract class “Is-A” base class. Objects that implement “Can-Do-This”. Now if I asked this question and got the answer, yes, that would be the correct answer. However, I want to know why one would want to use an interface over an abstract class, and vice versa.

credits : http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/

11/28/2009
 
 
If you have functionality that ALL derived classes should have, use an abstract class. If you want a "contract" that all classes will have certain features, use an interface.

12/04/2009
 
 
You can look at interface as another (partial) face of a class, a specific face that handles specific concerns of the class.
Abstract class is a base implementation of concerns that all derived classes share.
Since interfaces are polymorphic they provide essential tool to design a class with many concerns however without giving up the seperation of concerns within the same class.
The other way to achieve almost the same functionality would be to derive from more than one abstract class however this will cause two problems in your design:
1. what happens if two methods have the same name in your abstracts - which one will be the correct one to execute ?
2. deriving from a class which derives two classes will cause a join of concerns meaning that you no longer be able to distinguish between the functionality that each base class gave to the derived class.
To avoid this problems C# architects decided to provide interfaces and to constrain that a class can only derive from one class.

12/10/2009
 
 
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
abstract class MyBaseAbstract
{
public void M1()
{
int i = 10;
Console.WriteLine(i);
}
abstract public void M2();



}
class Program : MyBaseAbstract
{

public override void M2()
{
Console.WriteLine("I am OK");
}

static void Main(string[] args)
{
Program p = new Program();
p.M1();
p.M2();

}
}
}


interface

15
using System;
namespace ConsoleApplication2
{
public interface MyName
{
void Name();
}

class Program : MyName
{

#region MyName Members
public void Name()
{
Console.WriteLine("ahha");
}

#endregion

static void Main(string[] args)
{
Program p = new Program();
p.Name();

}


}
}

02/28/2010
 
 
Interface is a contract for the client i.e. user of a class. This class has implemented an interface.
Abstract class is a class which has defined as well as non-defined members(functions/properties etc).
The members which are non-defined should be preceded with 'abstract' keyword and so the class must be declared as abstract. Abstract class is a class that has no direct instances, but whose descendants may have direct instances.
Variables can be declared in abstract class, it is not allwed in interface. Interface contains only abstract members. Access modifiers are not allowed for interface members. Bydefault interface members are public virtual.
A class may inherit several interfaces but it can inherit only one abstract class.
Abstarct class can have constructor, interface cannot have constructor.
Abstract class is used as a root in inheritance hierarchy, whereas interface can be mixed up with hierarchy at any place.

02/28/2010
 
 
• Abstract class gives an Identity to the derived object.
• Interface extends the ability of an object.
• Interfaces give polymorphic behavior.
• Abstract class also gives polymorphic behavior. But it is more meaningful to say that Abstract class simplifies versioning, as the base class is flexible and inheriting class can create many versions of it by additional functions and also by implementing Interfaces.
• Interfaces and Abstract Classes both promote reusability.

02/28/2010
 
 
Add an answer*
 
Your name
Email
 
Location: United States
c# java .net Object oriented

add a question

arrow_blue


Now hiring!
---------------------------
---------------------------
---------------------------
Mercer 
---------------------------
---------------------------
---------------------------
---------------------------