Interface and Abstract


Abstract class:

abstract classes are the class where one or more methods are abstract but not necessarily all method has to be abstract. 
Abstract methods are the methods, which are declare in its class but not define. The definition of those methods must be in its extending class.


Interfaces are one type of class where all the methods are abstract. That means all the methods only declared but not defined. All the methods must be define by its implemented class.

Difference between Interface and Abstract
No. Interface Abstract class Features
1 A class may inherit several interfaces. A class may inherit only one abstract class. Multiple inheritance
2 An interface cannot provide any code, just the signature. An abstract class can provide complete, default code and/or just the details that have to be overridden. Default implementation
3 An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public An abstract class can contain access modifiers for the subs, functions, properties Access Modfiers
4 Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface. An abstract class defines the core identity of a class and there it is used for objects of the same type. Core VS Peripheral
5 If various implementations only share method signatures then it is better to use Interfaces. If various implementations are of the same kind and use common behaviour or status then abstract class is better to use. Homogeneity
6 Requires more time to find the actual method in the corresponding classes. Fast Speed
7 If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. Adding functionality (Versioning)
8 No fields can be defined in interfaces An abstract class can have fields and constrants defined Fields and Constants

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>