Quote:Abstract & Interface
• An abstract class can have shared state or functionality. An interface is only a promise to provide the state or functionality. A good abstract class will reduce the amount of code that has to be rewritten because its functionality or state can be shared. The interface has no defined information to be shared.
• Root point is that abstract classes ensure that all descendants share functionality/state, where an interface only ensures a common bond.
• Abstract classes allow you to create a blueprint, and allow you to additionally CONSTRUCT (implement) properties and methods you want ALL its descendants to possess.
• An interface on the other hand only allows you to declare that you want properties and/or methods with a given name to exist in all classes that implement it - but doesn't specify how you should implement it. Also, a class can implement MANY interfaces, but can only extend ONE Abstract class. An Interface is more of a high level architectural tool
• Why use one over the other? The former allows for a more concrete definition of descendants - the latter allows for greater polymorphism.
• Abstract classes can be Derived whereas Interfaces can be Implemented. There is some difference between the two. When you derive an Abstract class, the relationship between the derived class and the base class is 'is a' relationship. e.g., a Dog is an Animal; a Sheep is an Animal which means that a Derived class is inheriting some properties from the base class. Whereas for implementation of interfaces, the relationship is "can be". e.g., a Dog can be a spy dog. A dog can be a circus dog. A dog can be a race dog. This means that you implement certain methods to acquire something.
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)