none
difference between Pure abstract class and Interface?

    Question

  • hi,

    i just want to know the difference between pure abstract class(as same as interface- no implementation of methods) and interface

    Thanks,

    Tuesday, August 19, 2014 11:32 AM

Answers

  • Hi Bhagya,

    In summary and Very practical in the real world :



    Ref: Difference between abstract class and interface

    • Proposed as answer by Rigz Wednesday, August 20, 2014 10:10 AM
    • Marked as answer by CaillenModerator Tuesday, August 26, 2014 9:35 AM
    Wednesday, August 20, 2014 6:35 AM
  • Well, an interface is just a contract and an abstract class is still a class regardless of whether it contains any methods. An abstract class is more expensive to use because there is a lookup to do when you inherit from them. Please refer to the following page for more information: http://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class

    There are also some differences when it comes to multiple inheritance, access modifiers, etc. Please refer to the following page for more information: http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface

    If you don't need to define any behaviour (e.g. methods), you should use an interface instead of an abstract class.

    • Proposed as answer by Rigz Wednesday, August 20, 2014 10:10 AM
    • Marked as answer by CaillenModerator Tuesday, August 26, 2014 9:34 AM
    Tuesday, August 19, 2014 11:39 AM
  • There are a bunch of differences.

    I would tend to focus on:

    • Abstract classes can retain state and have actual code / implementation of methods. Interfaces can't.
    • You can only inherit from one class but Implement many interfaces.

    Amongst the weirder more technical ones:

    You don't need to implement all methods in a class inheriting from an abstract class. Although I think that still has to be an abstract class itself.

    .

    I tend to write interfaces a lot and abstract classes pretty rarely.


    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    • Proposed as answer by Mr. Zator Wednesday, August 20, 2014 10:26 AM
    • Marked as answer by CaillenModerator Tuesday, August 26, 2014 9:34 AM
    Tuesday, August 19, 2014 11:57 AM
    Moderator
  • I think by pure abstract class you mean something that is common in C++ so only methods without implementation and no data meaning no field or properties. So pure abstract class has no state because there is nothing where state can be saved.

    public abstract class Animal
    {
        protected abstract void Eat();
    }
    
    public interface IAnimal
    {
        void Eat();
    }

    When comparing those two above the most important differences are access modifiers and the fact that you can only inherit from single class, but implement many interfaces. And of course when talking about .NET everything that abstract class will inherit from System.Object so you might think that its not really an pure abstract class.

    Tuesday, August 19, 2014 12:30 PM

All replies

  • Well, an interface is just a contract and an abstract class is still a class regardless of whether it contains any methods. An abstract class is more expensive to use because there is a lookup to do when you inherit from them. Please refer to the following page for more information: http://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class

    There are also some differences when it comes to multiple inheritance, access modifiers, etc. Please refer to the following page for more information: http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface

    If you don't need to define any behaviour (e.g. methods), you should use an interface instead of an abstract class.

    • Proposed as answer by Rigz Wednesday, August 20, 2014 10:10 AM
    • Marked as answer by CaillenModerator Tuesday, August 26, 2014 9:34 AM
    Tuesday, August 19, 2014 11:39 AM
  • It's still a class. It may contain fields (members), constructors(destructors), access modifiers.
    Tuesday, August 19, 2014 11:42 AM
  • There are a bunch of differences.

    I would tend to focus on:

    • Abstract classes can retain state and have actual code / implementation of methods. Interfaces can't.
    • You can only inherit from one class but Implement many interfaces.

    Amongst the weirder more technical ones:

    You don't need to implement all methods in a class inheriting from an abstract class. Although I think that still has to be an abstract class itself.

    .

    I tend to write interfaces a lot and abstract classes pretty rarely.


    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    • Proposed as answer by Mr. Zator Wednesday, August 20, 2014 10:26 AM
    • Marked as answer by CaillenModerator Tuesday, August 26, 2014 9:34 AM
    Tuesday, August 19, 2014 11:57 AM
    Moderator
  • I think by pure abstract class you mean something that is common in C++ so only methods without implementation and no data meaning no field or properties. So pure abstract class has no state because there is nothing where state can be saved.

    public abstract class Animal
    {
        protected abstract void Eat();
    }
    
    public interface IAnimal
    {
        void Eat();
    }

    When comparing those two above the most important differences are access modifiers and the fact that you can only inherit from single class, but implement many interfaces. And of course when talking about .NET everything that abstract class will inherit from System.Object so you might think that its not really an pure abstract class.

    Tuesday, August 19, 2014 12:30 PM
  • Hi Bhagya,

    In summary and Very practical in the real world :



    Ref: Difference between abstract class and interface

    • Proposed as answer by Rigz Wednesday, August 20, 2014 10:10 AM
    • Marked as answer by CaillenModerator Tuesday, August 26, 2014 9:35 AM
    Wednesday, August 20, 2014 6:35 AM
  • Bhagya,

    Interface support multiple inheritance

    Interface does not support multiple inheritance, it just supports multiple implementation. A class, a struct can implement multiple interfaces but you can make an interface combination.

    Wednesday, August 20, 2014 9:17 AM
  • A very nice and detailed article on this code project link (posted by Magnus).
    • Edited by Rigz Wednesday, August 20, 2014 10:13 AM
    Wednesday, August 20, 2014 10:13 AM