In this section of blog, we will look into comparison between Interfaces and Abstract Classes. Generally, programmers often get confuse at the time of design that, when to use interfaces and when to use abstract classes. It all depends on class design. An abstract class is a class which is incomplete, it can contain data members, constructors, method definitions, static fields and methods. So it has already functionality defined as compared to interface. A class which extends an abstract class inherits all this. When say a class tyre extends an abstract class car, objects of class tyre are also of type car.
Interfaces are used when one or more classes need the functionality of the interface. It is not necessary that this class must have any relationship with interface. Let say we have two classes class Employee and class Rectangle, both class can implement Comparable interface for comparing their objects. But, none of the class Employee and Rectangle has any relationship with the Comparable interface.
Comparison between Interfaces and Abstract Classes
1. An Abstract class is an Incomplete class, whereas an Interface is a pure abstract class.
2. An Abstract class generally defines common behaviour and attributes that a class inherits, whereas an Interface specifies functionality which a class should have.
3. An Abstract class contains method declarations and definitions, whereas an Interface contains only method declarations.
4. An Abstract class can contain fields which can be static, final, as well as instance fields, where as Interface fields are implicitly final and static.
5. An Abstract class can have a constructor, whereas an Interface cannot have a constructor.
6. An Abstract class is used with extends keyword, whereas an Interface is used with implements keyword.
7. An Abstract class can be extended by more than one class, whereas an Interface can be implemented by more than one class.
8. A class can extend only one abstract class, whereas a class can implement more than one Interface.
9. An Abstract class can be extended by another abstract as well as concrete class, whereas an Interface can be extended by another Interface.
10. A class cannot extend more than one class, whereas an Interface can be used to implement multiple inheritance.