Monday, 4 August 2014

Interfaces and Abstract classes in C-Sharp

What is an Interface?

An interface is a reference type containing only abstract members. These can be events, indexers, methods or properties, but only the member declarations. A class implementing an interface must provide the implementation of the interface members. An interface cannot contain constants, constructors, data fields, destructors, static members or other interfaces. Interface member declarations are implicitly public.

The attributes is optional and is used to hold additional declarative information.
The modifier is optional. The allowed modifiers are public and internal, unless nested inside a class, then the allowed modifiers are publicprotectedprivateand internal. If no modifier is supplied then a default of internal is used. 
The keyword interface must be followed by an identifier that names the interface. 
The base-type of an interface can be zero or more interfaces. When more than one base-type is used, then this is a comma-separated list of base-types. 

What is an Abstract Class?

Like an interface, you cannot implement an instance of an abstract class, however you can implement methods, fields, and properties in the abstract class that can be used by the child class.

The override keyword tells the compiler that this method was defined in the base class.

There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison:
FeatureInterfaceAbstract class
Multiple inheritanceA class may inherit several interfaces.A class may inherit only one abstract class.
Default implementationAn interface cannot provide any code, just the signature.An abstract class can provide complete, default code and/or just the details that have to be overridden.
Access Modfiers
An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public
An abstract class can contain access modifiers for the subs, functions, properties
Core VS PeripheralInterfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.An abstract class defines the core identity of a class and there it is used for objects of the same type.
HomogeneityIf various implementations only share method signatures then it is better to use Interfaces.If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
SpeedRequires more time to find the actual method in the corresponding classes.Fast
Adding functionality (Versioning)If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Fields and Constants
No fields can be defined in interfaces
An abstract class can have fields and constrants defined


Some important syntaxes:

1. You can inherent one abstract class and one interface to the same class, but first you have to inherit abstract class and next inherit Interface.

   public interface Iclass
    {
    }
    public abstract class Abstract
    {
    }
    public class Class3 : AbstractIclass //Correct
    {  
    }
2. You can’t inherit multiple base classes to the one class

   public  class Class1
    {
    }
    public class Class2
    {
    }
    public class Class3 : Class1,Class2 //Error
    {
    }
3. You can inherit intermediate base class

    public  class Class1
    {
    }
    public class Class2 : Class1
    {
    }
    public class Class3 : Class2
    {
    }

Summery:
  • An Interface cannot implement methods.
  • An abstract class can implement methods.
  • An Interface can only inherit from another Interface.
  • An abstract class can inherit from a class and one or more interfaces.
Example:
    public interface Iclass
    {
    }
    public interface Iclass1:Iclass
    {
    }
    public abstract class Abstract:Iclass ,Iclass1
    {
    }
  • An Interface cannot contain fields.
  • An abstract class can contain fields.
  • An Interface can contain property definitions.
  • An abstract class can implement a property.
  • An Interface cannot contain constructors or destructors.
  • An abstract class can contain constructors or destructors.
  • An Interface can be inherited from by structures.
  • An abstract class cannot be inherited from by structures.
  • An Interface can support multiple inheritance.
  • An abstract class cannot support multiple inheritance.
Example:
    public class Class3 : Iclass1 , Iclass
    {
    }
    Public abstract class Class3 : class1 ,class //Error
    {
    }


No comments:

Post a Comment