What is the difference between abstract class and interfaces?

Before taking look at the differences between the abstract class and interface we need to understand what are the abstract classes and interface.

Abstract Class :

1. Abstract class is declared abstract.
2. It may or may not contain abstract methods.
3. Abstract classes cannot be instantiated.
4. Abstract class can be subclassed.
5. Define some behaviors which are forced to subclasses to provide other.

So considering all the points above, when we make inheritance structure we need to understand which classes we need to make abstract and which are concrete.
Here is an example of Animal class which is declared abstract and which defines some behavior and there are animals like cat and dog which shares some common behavior among them along with their specific characteristics. So we can say Animal is abstract class and Cat and Dog are more of concrete classes.


Interface :
1. In java interface is more of a reference type.
2. Contain method signature ( with no actual body ), constants and nested types.
3. Interfaces cannot be instantiated, implemented by interfaces or extended by abstract classes.
4. In Java, a class can inherit from only one class but interface can be implemented more than once. Therefore, objects can have multiple types: the type of their own class and the types of all the interfaces that they implement, which helps in multiple inheritance.


Now lets see the difference between the two :

1. Abstract classes can be used to consolidate and share some functionality but in case of interfaces there are some differences like (i)Interfaces are used to specify common functionality which is shared between different instances.(j) Interfaces doesnt build the actual functionality.

2. Interfaces are more cleaner , when we "implement" an interface, in this case class which implements the interface adheres to the "contract" of the interface. In case of class inheritance sub-class adheres to the "contract" of the interface of the parent class which makes it less cleaner than interface.

3.Abstract classes can contain fields which are not static and final, and they can contain implemented methods. In interface we dont find this. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.

4.Classes can implement multiple interfaces but not abstract class.

5. Abstract classes are used Used only when "is-a" relationship is there but interfaces are implemented by classes which are not related with each other.

6. When there is a change in design use interfaces and when you want to have default behaviors abstract classes is the best option e.g. Abstract Classes -> Application frameworks
Interfaces -> Comparable interface.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment