Pages

Men

rh

4/28/2012

Abstract Classes vs Interface

Abstract Function:
A function which contains only Declaration / Signature and does not contain Implementation/ Body/Definition is known as Abstract Function. To make a function as  Abstract  use Abstract keyword.

If you declare abstract function that should be over write in the derived class.

Example
Abstract class clsEmployee
{
   Public abstract void GetempData ();
  Public virtual void  DisplayEmpData ();
}

Class clsManager: clsEmployee
{
   Public override void GetempData ()
    {
    Console.Write (“Employee data”);       
    }
    public override void DisplayEmpData ()
    {
    Console.Write(“Display Employee data”);
    }
}

Abstract Class:
  •   A class which contains one or more abstract functions is known as Abstract Class.
  •  To make a class as Abstract, use Abstract keyword.
  •  Abstract class cannot be initiated.
  •  An abstract class can contain non abstract functions.
  •  An abstract class can contains all members of its class.
  •  Abstract class can contain constructors, fields, properties and methods like any other class
Interface:
 
Interfaces describe a group of related functionalities that can belong to any class or struct.
You define an interface by using the interface keyword, as shown in the following example.
 
Description:
  • Interfaces consist of methods, properties, events, indexers, or any combination of those four member types.
  • An interface cannot contain constants, fields, operators, instance constructors, destructors, or types.
  • It cannot contain static members.
  • Interfaces members are automatically public, and they cannot include any access modifiers.
  • When a class or struct implements an interface, the class or struct provides an implementation for all of the members defined by the interface.
  • The interface itself provides no functionality that a class or struct can inherit in the way that base class functionality can be inherited.
  • However, if a base class implements an interface, the derived class inherits that implementation. The derived class is said to implement the interface implicitly.
  • Classes and structs implement interfaces in a manner similar to how classes inherit a base class or struct, with two exceptions:
  • A class or struct can implement more than one interface.
  • When a class or struct implements an interface, it receives only the method names and signatures, because the interface itself contains no implementations, as shown in the following example.
Example:
 
interface IEquatable<T>
{
    bool Equals(T obj);
}
 
public class Car : IEquatable<Car>
{
    public string Make {get; set;}
    public string Model { get; set; }
    public string Year { get; set; }
 
    // Implementation of IEquatable<T> interface
    public bool Equals(Car car)
    {
        if (this.Make == car.Make &&
            this.Model == car.Model &&
            this.Year == car.Year)
        {
            return true;
        }
        else
            return false;
    }
}
 
An interface has the following properties:
  •  An interface is like an abstract base class: any non-abstract type that implements the interface must implement all its members.
  • An interface cannot be instantiated directly.
  • Interfaces can contain events, indexers, methods, and properties.
  • Interfaces contain no implementation of methods.
  • Classes and structs can implement more than one interface.
  • An interface itself can inherit from multiple interfaces.
Benefits of Interface :
  •  By using Interface we can implement Polymorphic behavior.
  • Can achieve multiple inheritances.
  • Can implement loosely Coupled Systems.
  • Can implement Dependency Injection.
  • It enables parallel application development.
Polymorphic behavior: 
It is simply the act of using set of Interface members defined for a particular class’s (or Interface’s) derived classes.Polymorphic behavior is achieved in a program by targeting a set of operations specified by a class or interface and manipulating their derived class objects via those operations.

Difference between Abstract Class and interface:
Abstract Class:
  • A Class which contains one or more Abstract Function is known as Abstract Class
  • An Abstract class can contains non abstract functions.
  • An Abstract class can contain all members of its class.
  • Use Abstract keyword to create an Abstract class.
  • We cannot implement multiple inheritances.
  • By default Abstract class members are non public and not abstract.
  • Abstract class cannot be initiated directly.
  • Create a new class from an abstract class is must consume it.
Interface:
  • A class which contains all abstract methods known as Interface.
  • An Interface does not contain non abstract functions.
  • An Interface can contain only Abstract properties, Indexes, Events.
  • Use Interface keyword to create an Interface.
  • We can implement multiple inheritances.
  • By default all interface members are public and abstract.
  • An Interface cannot be initiated directly.
  • Create a new class from an interface must to consume it.
In which scenario we use Abstract Classes and Interfaces
Interface:
If your child classes should all implement a certain group of methods/functionalities but each of the child classes is free to provide its own implementation then use interfaces.
1For e.g. if you are implementing a class hierarchy for vehicles implement an interface called Vehicle which has properties like Color MaxSpeed etc. and methods like Drive(). All child classes like Car Scooter AirPlane SolarCar etc. should derive from this base interface but provide a separate implementation of the methods and properties exposed by Vehicle.
If you want your child classes to implement multiple unrelated functionalities in short multiple inheritance use interfaces.
For e.g. if you are implementing a class called SpaceShip that has to have functionalities from a Vehicle as well as that from a UFO then make both Vehicle and UFO as interfaces and then create a class SpaceShip that implements both Vehicle and UFO .
Abstract Classes
When you have a requirement where your base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes use abstract classes.
For e.g. again take the example of the Vehicle class above. If we want all classes deriving from Vehicle to implement the Drive() method in a fixed way whereas the other methods can be overridden by child classes. In such a scenario we implement the Vehicle class as an abstract class with an implementation of Drive while leave the other methods / properties as abstract so they could be overridden by child classes.
 The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
For example a class library may define an abstract class that is used as a parameter to many of its functions and require programmers using that library to provide their own implementation of the class by creating a derived class.

Use an abstract class

  • When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. ( COM was designed around interfaces.)
  • Use an abstract class to define a common base class for a family of types.
  • Use an abstract class to provide default behavior.
  • Subclass only a base class in a hierarchy to which the class logically belongs.

Use an interface

  • When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.
  • Use interfaces to introduce polymorphic behavior without sub classing and to model multiple inheritance—allowing a specific type to support numerous behaviors.
  • Use an interface to design a polymorphic hierarchy for value types.
  • Use an interface when an immutable contract is really intended.
  • A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.

What is Implicit and Explicit Interface Implementation?
the concept of implicit and explicit implementation provide safe way to implement methods of multiple interfaces by hiding, exposing or preserving identities of each of interface methods, even when the method signatures are the same.

Let's consider the interfaces defined below.
interface IDisposable
{
    void Dispose();
} 

Here you can see that the class Student has implicitly and explicitly implemented the method named Dispose() via Dispose and IDisposable.Dispose.
class Student : IDisposable
{
    public void Dispose()
    {
        Console.WriteLine("Student.Dispose");
    }
 
    void IDisposable.Dispose()
    {
        Console.WriteLine("IDisposable.Dispose");
    }






















No comments :

Post a Comment