|
Answer» An abstract class is a special kind of class that can be inherited but cannot be instantiated. For a class to be an abstract class, there should always be at least one abstract METHOD should be present. Interface is not a class, it is an entity that is defined by the work Interface. Like Abstract class we cannot create an instance of Interface. It has no implementation; only has the signature i.e. just the definition of the methods without the body. Advantage of Interface is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface. Difference between Interface and Abstract Class Feature
| Interface | Abstract class |
|---|
Multiple inheritance
| A class can inherit to multiple interfaces | A class may inherit from only one abstract class. | | Default implementation | An interface only have method declaration, no definition. | An abstract class can have methods with complete definition or abstract methods that to be overriden in derived class
| | Access Modifiers | for interface everything is assumed as public | An abstract class have access modifiers for the subs, functions, properties
| | Homogeneity | Interfaces are better OPTION when various IMPLEMENTATIONS share the same method signature. | Abstract classes are better when various implementations are of the same kind and use common behaviour or status.
| | Adding functionality (Versioning) | Adding a new method to interface need to implemented in derived classes | while adding new method we have the option of providing default implementation and therefore all the existing code might work properly.
| | Fields and CONSTANTS | No fields can be defined | can have fields and constants defined
|
|