1.

What is the difference between Interface and Abstract class?

Answer»
  • Abstract Class

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

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
InterfaceAbstract class
Multiple inheritance
A class can inherit to multiple interfacesA class may inherit from only one abstract class.
Default implementationAn 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 Modifiersfor interface everything is assumed as publicAn abstract class have access modifiers for the subs, functions, properties
HomogeneityInterfaces 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 classeswhile adding new method we have the option of providing default implementation and therefore all the existing code might work properly.
Fields and CONSTANTSNo fields can be definedcan have fields and constants defined


Discussion

No Comment Found