There
are quite a big difference between an interface and
an abstract
class,
even though both look similar.
- Interface definition begins with a keyword interface so it is of type interface
- Abstract classes are declared with the abstract keyword so it is of type class
- Interface has no implementation, but they have to be implemented.
- Abstract class’s methods can have their own default implementations and they may be extended. The Abstract class’s methods could run independent of the inheriting class.
- Interfaces can only have method declaration (implicitly public and abstract) and properties (implicitly public static)
- Abstract class’s methods can’t have implementation only when declared abstract.
- Interface can inherit more than one interfaces
- Abstract class can implement more than one interfaces, but can inherit only one class
- Abstract class must override all abstract method and may override virtual methods
- Interface can be used when the implementation is changing
- Abstract class can be used to provide some default behavior for a base class.
- Interface makes implementation interchangeable
- Interface increase security by hiding the implementation
- Abstract class can be used when implementing framework
- Abstract classes are an excellent way to create planned inheritance hierarchies and also to use as non-leaf classes in class hierarchies.
Abstract
classes let you define some behaviors; they force your sub classes to
provide others. For example, if you have an application framework, an
abstract class can be used to provide the default implementation of
the services and all mandatory modules such as event logging and
message handling etc. This approach allows the developers to develop
the application within the guided help provided by the framework.
However,
in practice when you come across with some application-specific
functionality that only your application can perform, such as startup
and shutdown tasks etc. The abstract base class can declare virtual
shutdown and startup methods. The base class knows that it needs
those methods, but an abstract class lets your class admit that it
doesn't know how to perform those actions; it only knows that it must
initiate the actions. When it is time to start up, the abstract class
can call the startup method. When the base class calls this method,
it can execute the method defined by the child class.
No comments:
Post a Comment