locked
whats the advantages of interface over abstract class? RRS feed

  • Question

  • I know the difference between interface and abstract class and i know we can use commom funtionality in abstract class, what i want to know is "what is the advantage of interface over abstract class?"
    Tuesday, December 16, 2008 6:08 PM

Answers

  • Abstract Classes Interface
    Cannot be instantiated independently from their derived classes. Abstract class constructors are called only by their derived classes. Cannot be instantiated.
    Define abstract member signatures that base classes must implement. Implementation of all members of the interface occurs in the base class. It is not possible to implement only some members within the implementing class.
    Are more extensible than interfaces, without breaking any version compatibility. With abstract classes, it is possible to add additional nonabstract members that all derived classes can inherit. Extending interfaces with additional members breaks the version compatibility.
    Can include data stored in fields. Cannot store any data. Fields can be specified only on the deriving classes. The workaround for this is to define properties, but without implementation.
    Allow for (virtual) members that have implementation and, therefore, provide a default implementation of a member to the deriving class. All members are automatically virtual and cannot include any implementation.
    Deriving from an abstract class uses up a subclass's one and only base class option. Although no default implementation can appear, classes implementing interfaces can continue to derive from one another.
    • Proposed as answer by ronan001 Tuesday, December 16, 2008 7:18 PM
    • Marked as answer by Guo Surfer Friday, December 19, 2008 10:10 AM
    Tuesday, December 16, 2008 7:15 PM
  • One advantage is that value types can implement interfaces, but they cannot inherit from abstract classes.
    David Morton - http://blog.davemorton.net/
    • Proposed as answer by ronan001 Tuesday, December 16, 2008 6:58 PM
    • Marked as answer by Guo Surfer Friday, December 19, 2008 10:10 AM
    Tuesday, December 16, 2008 6:13 PM
  • Interfaces allow you to provide particular sections of classes within a class hierarchy to have certain abilities.

    So if you had all classes in your game inheriting from an abstract class vehicle, lets say tank, plane, rocket are three that inherit from vehicle.

    You might also want to enforce a fly method on plane and rocket, you shudnt include it in your base class vechicle, because then the tank could fly. So you can use interface that forces only plane and rocket to implement the method fly.
    • Proposed as answer by ronan001 Tuesday, December 16, 2008 6:58 PM
    • Marked as answer by Guo Surfer Friday, December 19, 2008 10:10 AM
    Tuesday, December 16, 2008 6:51 PM
  • Another advantage is that one class may implement several interfaces while it can only inherit from one abstract class.
    • Proposed as answer by ronan001 Tuesday, December 16, 2008 6:58 PM
    • Marked as answer by Guo Surfer Friday, December 19, 2008 10:10 AM
    Tuesday, December 16, 2008 6:52 PM

All replies

  • One advantage is that value types can implement interfaces, but they cannot inherit from abstract classes.
    David Morton - http://blog.davemorton.net/
    • Proposed as answer by ronan001 Tuesday, December 16, 2008 6:58 PM
    • Marked as answer by Guo Surfer Friday, December 19, 2008 10:10 AM
    Tuesday, December 16, 2008 6:13 PM
  • Interfaces allow you to provide particular sections of classes within a class hierarchy to have certain abilities.

    So if you had all classes in your game inheriting from an abstract class vehicle, lets say tank, plane, rocket are three that inherit from vehicle.

    You might also want to enforce a fly method on plane and rocket, you shudnt include it in your base class vechicle, because then the tank could fly. So you can use interface that forces only plane and rocket to implement the method fly.
    • Proposed as answer by ronan001 Tuesday, December 16, 2008 6:58 PM
    • Marked as answer by Guo Surfer Friday, December 19, 2008 10:10 AM
    Tuesday, December 16, 2008 6:51 PM
  • Another advantage is that one class may implement several interfaces while it can only inherit from one abstract class.
    • Proposed as answer by ronan001 Tuesday, December 16, 2008 6:58 PM
    • Marked as answer by Guo Surfer Friday, December 19, 2008 10:10 AM
    Tuesday, December 16, 2008 6:52 PM
  • Abstract Classes Interface
    Cannot be instantiated independently from their derived classes. Abstract class constructors are called only by their derived classes. Cannot be instantiated.
    Define abstract member signatures that base classes must implement. Implementation of all members of the interface occurs in the base class. It is not possible to implement only some members within the implementing class.
    Are more extensible than interfaces, without breaking any version compatibility. With abstract classes, it is possible to add additional nonabstract members that all derived classes can inherit. Extending interfaces with additional members breaks the version compatibility.
    Can include data stored in fields. Cannot store any data. Fields can be specified only on the deriving classes. The workaround for this is to define properties, but without implementation.
    Allow for (virtual) members that have implementation and, therefore, provide a default implementation of a member to the deriving class. All members are automatically virtual and cannot include any implementation.
    Deriving from an abstract class uses up a subclass's one and only base class option. Although no default implementation can appear, classes implementing interfaces can continue to derive from one another.
    • Proposed as answer by ronan001 Tuesday, December 16, 2008 7:18 PM
    • Marked as answer by Guo Surfer Friday, December 19, 2008 10:10 AM
    Tuesday, December 16, 2008 7:15 PM