Abstract Class vs Interface

Well, I can say that interface is for "PURE ABSTRACTION". Pure abstraction means you can not have concrete methods. But an abstract class can have concrete methods. Interfaces should not have any concrete methods, it should only have method declarations.
I will explain the differences between the abstract class and Interface in an old-fashioned tabular format –

Abstract Class
It can have concrete methods
        It can only have abstract methods
It can have variables of any access specifier
It can have only public static final data members(i.e. only constant)
Concrete methods can have any access specifier
For interfaces, all methods are public and abstract by default
A class can extend only one abstract class
A class can implement any no. of interfaces

Abstract classes are meant to be inherited from, and when one class inherits from another it means that there is a strong relationship between the 2 classes.
An abstract class would be better to use or I can say it would be more appropriate when there is a strong relationship between the abstract class and the classes that will derive from it. This is because an abstract class is very closely linked to inheritance, which signifies a strong relationship. But, with interfaces there need not be a strong relationship between the interface and the classes that implement the interface.
In Java, a class can only derive from one class, whether it’s abstract or not. However, a class can implement multiple interfaces – which could be considered as an alternative to for multiple inheritance. So, one major difference between the two is that a Java class can inherit from only one abstract class, but can implement multiple interfaces.

At times there is a lot of confusion among the developers regarding which should one use, abstract classes or interfaces?

Below are some of the scenarios where one can consider using either the abstract class or an interface -

·         Consider using abstract classes if any of these statements applies:
o You want to share code among several closely related classes.
o You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
o You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

·         Consider using interfaces if any of these statements applies:
o You expect that unrelated classes would implement your interface.
o You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
o You want to take advantage of multiple inheritance of type.

Use abstract classes when you want common behavior for same category classes.
Use interfaces when you want common behavior for different category classes.

In simple words,
When you want to develop some defined methods and some undefined methods as a common for multiple developers then use abstract class, otherwise if you want give only definition of methods for multiple developers as a common methods in such a way that according to their need they will implement those methods then use interface... In simple terms Abstract class contains some implemented methods and some unimplemented methods, where as interface contains only unimplemented methods....

Here are some guidelines on when to use an abstract class and when to use interfaces in Java programming:
·   An abstract class is good if you think you will plan on using inheritance since it provides a common base class implementation to derived classes.
·   An abstract class is also good if you want to be able to declare non-public members. In an interface, all methods must be public.
·   If you think you will need to add methods in the future, then an abstract class is a better choice. Because if you add new method headings to an interface, then all of the classes that already implement that interface will have to be changed to implement the new methods. That can be quite a hassle.
·  Interfaces are a good choice when you think that the API will not change for a while.
· Interfaces are also good when you want to have something similar to multiple inheritance, since you can implement multiple interfaces.

I think that interfaces should be used when we need to define a common behaviour which can have different variations of implementation, but abstract classes are used when we have some algorithmic steps/template for a behaviour that should be followed by all subclasses irrespective of each step implementation in the algorithm.
Interfaces are just the blueprints whereas abstract class include partial implementation. Obviously we can do our job without using interfaces and only using abstract classes but this will be bad practice because the code will NOT be modular.

Post a Comment