tags:

views:

157

answers:

7

I read some points about interface:

" Interface is the definition of contract that a class must conform.It does not inherit anything. "

while defining the differece between interface and abstract class can i say :

when a class is derived from abstract class it is known as real inheritance.When a class uses interface then it is not inheritance,it is an implementation of contracts ?

+1  A: 

When a class uses interface then it is not inheritance,it is an implementation of contracts

Yes, that is exactly how it is.

This may be a bit clearer if you look into what happens when you have one interface inheriting another one. That may be confusing since we just said that with interface it's not inheritance but just a contract definition. If you take the following code:

interface IA
{
    void MethodA();
}

interface IB : IA
{
    void MethodB();
}

class B : IB
{
    public void MethodA() { }
    public void MethodB() { }

}

class AB : IA, IB
{
    public void MethodA() { }
    public void MethodB() { }
}

If you examine the IL code of the classes B and AB (using ildasm or similar) you will find that both classes are identical when it comes to which interfaces they implement. The interface IB works as a "shortcut" to include also the interface IA, but there is no inheritance in the traditional sense involved.

Fredrik Mörk
+1  A: 

when a class is derived from abstract class it is known as real inheritance.When a class uses interface then it is not inheritance,it is an implementation of contracts ?

  • Yes
Mahin
+1  A: 

The difference between an abstract class and an interface is that an interface does not define the implementation of any of this methods, and an abstract class provides some implementation. That's the only difference. The difference between an abstract class and a concrete class is that an abstract class has at least one member for which an implementation is not provided.

Interfaces can derive from other interfaces.

Multiple inheritance is only allowed for interfaces due to reasons which are complicated and part of the religious wars of a previous era.

Paul Keister
Actually, technically, an abstract class is not required to contain at least one member that contains no implementation. An abstract class may define all its methods with implementation, and simply mark them as virtual. The difference is that an interface may NEVER provide an implementation, and therefore specifies a contract that an implementing class MUST abide by.
Mike Hofer
Fair enough, but a base class also defines a contract if it contains abstract methods. If an abstract class had no abstract methods, the designation would be superfluous. Fiven that I'm advocating a 'pragmatist' viewpoint on this topic, I'll accept that a class marked as abstract can be called abstract, even if it isn't. In a semantically proper frame of reference in which interface implementation can't be called, "inheritance," I would think it's also improper refer to a class with no abstract methods as being an abstract class.FYI I'm all for clear definition of contracts.
Paul Keister
+3  A: 

yes you are right . interface is just a contract to class.

note : Interfaces don’t derive from any System.Object-derived type. An interface is simply an abstract type that consists of a set of virtual methods, each with its own name, parameters, and return type. Interface methods can’t contain any implementation; hence, interface types are incomplete(abstract).

e.g :

interface I1
{
}
interface I2
{
}

abstract class a1
{
}
abstract class a2
{

}
class App:a1,I1,I2  //no Error
{   
static void Main(string[] args)
{       
}
}

note : The CLR allows a type to inherit from only one other type, which has System.Object as its root base type

       interface I1
        {
        }
        interface I2
        {
        }

        abstract class a1
        {
        }
        abstract class a2
        {

        }
        class App:a1,a2,I1,I2  //  Error
        {   
        static void Main(string[] args)
        {       
        }
        }
anishmarokey
+1  A: 

Yes ..you are right just to summarize

What is an Abstract Class?

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.

What is an Interface?

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.

Both Together

When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

JJ
A: 

An interface is defining an interface. Just think of the word interface.

Simple.

It defines how you interact (interface!) with the class.

Just reroute your brain back around upon itself and you see the answer was right there in front of you.

Alex Baranosky
+1  A: 

The difference between an abstract class and an interface is best understood if you research the use of the two keywords in question:

Essentially, an interface is a special construct that defines a code contract. It can contain no implementation whatsoever when you create it; it simply specifies the signatures of the properties and methods that the contract requires. When a class implements the interface, it must provide every property and method exactly as it is specified in the contract. It is the responsibility of the implementing class to provide the implementation.

An abstract class, however, is free to provide as much or as little implementation as it wants or needs to. It may, itself, implement an interface. All that is required of an abstract class is that it is marked abstract. Its members can be overriden by derived classes by marking the methods virtual in the abstract (base) class, and using the overrides keyword in the derived class. But the abstract base class is under no obligation to provide any functionality at all; nor is it under any obligation to provide a minimal number of virtual methods that lack implementation.

Hope this helps!

Mike Hofer