Feb 11, 2010

Difference between abstract classes and interfaces in C#

Abstract Classes

- In abstract class a method must be declared as abstract
- For abstract methods there will be no body
- The Abstract methods can delare with Access modifiers(public,internal,protected etc)But when implementing in subclass same access modifier we have to use.
- The Abstract class can contain variables and concrete methods
- Multiple inheritance is not possible
- must and should call all the abstract methods in the derived class
- To implement abstract method in derived class we have to use override keyword
- The accessmodifiers and return types(like void,int etc) must be same in abstract method and in derived class method
- we cannot create an object to abstract classes


Interfaces

- For interface methods also there will be no body
- In Interfaces we cannot use any access modifiers.By default these methods are public.
- We cannot declare variables and concrete methods in interfaces
- Multiple inheritance is possible
- must and should call all the interface methods in the derived class
- To implement interface methods in derived class no need of override keyword
- The accessmodifiers and return types(like void,int etc) must be same in interface methods and in derived class methods
- we cannot create an object to interfaces

5 comments:

  1. Abstract methods Vs Virtual methods

    - Abstract methods doesn't have implementation and forced derived classes to implement it
    - Virtual methods can have implementation and option to override in derived classes.

    ReplyDelete
  2. Abstract Example
    =================

    public abstract class Shape
    {
    //...Class implementation

    public abstract void Draw(int x, int y)
    {
    //this method mustn't be implemented here.
    //If we do implement it, the result is a Syntax Error.
    }
    }


    public abstract class Shape2D : Shape
    {
    //...Class implementation
    //...you do not have to implement the the method Draw(int x, int y)
    }

    public class Cricle : Shape2D
    {
    //here we should provide an implemetation for Draw(int x, int y)
    public override void Draw(int x, int y)
    {
    //must do some work here
    }

    }

    ReplyDelete
  3. When to use abstract and when to use interface

    - In case of Abstract class we can define COMMON functionalities in super class and those can be used in the derived class where as in Interface we cant do that. ( this i would say as advantage of abstract class)

    In case of Interface the derived class can implement any number of interface but restricted to extend only one abstract class (this i would say as advantage of Interface)

    ReplyDelete
  4. If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
    If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
    If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
    If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

    ReplyDelete
  5. A class inherits from 2 interfaces and both the interfaces have the same method name as shown below. How should the class implement the drive method for both Car and Bus interface?
    namespace Interfaces
    {
    interface Car
    {
    void Drive();
    }
    interface Bus
    {
    void Drive();
    }

    class Demo : Car,Bus
    {
    //How to implement the Drive() Method inherited from Bus and Car
    }
    }

    To implement the Drive() method use the fully qualified name as shown in the example below. To call the respective interface drive method type cast the demo object to the respective interface and then call the drive method.

    using System;
    namespace Interfaces
    {
    interface Car
    {
    void Drive();
    }
    interface Bus
    {
    void Drive();
    }

    class Demo : Car,Bus
    {
    void Car.Drive()
    {
    Console.WriteLine("Drive Car");
    }
    void Bus.Drive()
    {
    Console.WriteLine("Drive Bus");
    }

    static void Main()
    {
    Demo DemoObject = new Demo();
    ((Car)DemoObject).Drive(); //called Explicitly Implemeting an Interface
    ((Bus)DemoObject).Drive();
    }
    }
    }

    ReplyDelete