Results 1 to 9 of 9

Thread: Difference between Abstract classes and Interfaces in C#

  1. #1
    Join Date
    Jan 2009
    Posts
    8

    Difference between Abstract classes and Interfaces in C#

    Can any one please tell me what is the difference between Abstract classes and Interfaces in C# ?

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Difference between Abstract classes and Interfaces in C#

    An abstract class is a class that can not be instantiated but that can contain code.
    An interface only contains method definitions but does not contain any code. With an interface, you need to implement all the methods defined in the interface.

    If you have logic that will be the same for all the derived classes, it is best to go for a abstract class in stead of an interface.

    You can implement multiple interfaces but only inherit from one class.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Difference between Abstract classes and Interfaces in C#

    An abstract class can contain internal member variables, and can contain basic behaviour in the form of methods that have been coded.

    An interface can have neither of the two.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    What is an Abstract Class and interface?

    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.

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Example for Interface and Abstract class

    Example for Interface :

    Code:
    public interface IVehicle
    {
      void Start();
      void Drive();
      void Park();
      void ChangeGear(int gear);
      void SwitchOff();
    }
    public class Vehicle : IVehicle
    {
      public void Start()
      {
        Console.WriteLine("The vehicle has been started");
      }
     
      public void Drive()
      {
        Console.WriteLine("The vehicle is being driven");
      }
     
      public void Park()
      {
        Console.WriteLine("The vehicle is being parked");
      }
     
      public void ChangeGear(int gear)
      {
        Console.WriteLine("Gear changed to " + gear.ToString());
      }
     
      public void SwitchOff()
      {
        Console.WriteLine("The vehicle has been switched off");
      }
    }

    Example for Abstract class :

    Code:
    public abstract class Vehicle
    {
      public void Start()
      {
        Console.WriteLine("The vehicle has been started");
      }
     
      public abstract void Drive();
      public abstract void Park();
      public abstract void ChangeGear(int gear);
     
      public void SwitchOff()
      {
        Console.WriteLine("The vehicle has been switched off");
      }
    }
    
    public class Car : Vehicle
    {
      public Car()
      {
      }
     
      public override void Drive()
      {
        Console.WriteLine("The car is being driven");
      }
     
      public override void Park()
      {
        Console.WriteLine("The car is being parked");
      }
     
      public override void ChangeGear(int gear)
      {
        Console.WriteLine("The car changed gear changed to " + gear.ToString());
      }
    }

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: Difference between Abstract classes and Interfaces in C#

    Following are three fundamental difference between interfaces and abstract base :

    1. Abstract base classes can include code (methods).

    2. Abstract base classes can include data (fields).

    3. You can multiply-inherit from interfaces, but you cannot multiply-inherit from abstract base classes.

  7. #7
    Join Date
    May 2009
    Posts
    539

    Re: Difference between Abstract classes and Interfaces in C#

    There is a basic difference that you can check. If you are working with library for external clients you have to use abstract classes for that purpose. This have advantages as they keep getting newer version. This gives you option to add new members in abstract class without manipulating the current code. Now if talk about the interface we need to manipulate the existing code to add new interface. The older version needs to be replaced for upgrades. So there are less chances that what you want to implement might not work for longer time.

  8. #8
    Join Date
    Mar 2010
    Posts
    145

    Re: Difference between Abstract classes and Interfaces in C#

    Abstract class is better for implementing newer details. While interface lack this support. So here in one word if I say Interface classes are static and there are minor features that can be used for future manipulation the code. In comparison to that abstract classes needs the user library to be work on the base of other class. You can simply implement the interface on it. So in short there are lesser restrictions on it.

  9. #9
    Join Date
    May 2012
    Posts
    7

    Re: Difference between Abstract classes and Interfaces in C#

    abstract class contain the internal member variables, and basic behavior in the form of methods that have been coded.

Similar Threads

  1. Table and abstract classes
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 10:49 AM
  2. Use of abstract classes
    By ScarFace 01 in forum Software Development
    Replies: 5
    Last Post: 25-01-2010, 09:07 AM
  3. What is the difference between delegates and interfaces?
    By Rum in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 02:00 PM
  4. Abstract Classes in PHP5
    By nonose in forum Software Development
    Replies: 2
    Last Post: 16-06-2009, 02:33 PM
  5. Replies: 4
    Last Post: 02-03-2009, 08:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,439,359.88540 seconds with 17 queries