Java77: Difference Between Interface and Abstract Class : Java

Translate

Difference Between Interface and Abstract Class

The idea to introduce the interface came with one of the OOPs concept which was missing in the Java i.e Multiple Inheritance . As we know that Java does not support the multiple inheritance which means a class can extend only one parent class, hence Interface came into existence.

Abstract class comes with he concept where we can have the implementation of some of its methods however we can not provide the implementation of the methods in the interface

We need to decide whether to use the Abstract class or the Interface based on their pros and cons.
For example If we are choosing an abstract class over an interface than we are loosing the chance to extend another class, however we can implement multiple interfaces to achieve the multiple capability. One of the best example for choosing the interface over abstract class is Thread vs Runnable . If we want to execute a task and need run() method it's better to implement Runnable interface than extending Thread class which allows the class to extend any other parent class while utilizing the flexibility of working on threads.

Below are the main differences between Abstract Class and the Interface :

1. A Java abstract class can have instance methods that implements a default behavior however Java
    interface are by default abstract and these cannot have implementations i.e. Interface in can only contains
    declarations.

2. Interface does not have the constructors as these are used as a way of enforcing policy. An interface
    cannot be constructed. It can only be implemented or declared by the implementing class

   However Abstract class has the constructor like any other class. Constructors do not create an instance of
   the class. They are called as part of the object creation process. Their job is to initialize the object's          
   instance variables.
   However, if we create an instance of a class that extends an Abstract class, compiler will initialize both the
   classes. Here compiler will implicitly call the constructor of the Abstract class.

3. Abstract class allows to extend only one class because Java does not support multiple inheritance       
    however a class can implement multiple inheritance.

4. Variables declared in an interface are by default final and static. However an abstract class may contain
    non-final variables as well.

5. To implement an interface the keyword "implements" is used however for extending a class we need to
    use "extends" keyword.

6. An interface has all its members as 'public' by default, however abstract class allows any type of access 
    modifiers for its class members like private, public, protected etc.

7. Interface is completely abstract and it cannot be instantiated.
    A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.

8. Interfaces are slower than the abstract classes as it requires extra indirection.
    Interface involves a extra indirection before calling any overridden method in Java. 







5 comments:

  1. Nice one. It is really ciontaining good differnce at one place.

    ReplyDelete
  2. this is nice in term of concept wise.Please post some programatical difference.

    ReplyDelete
  3. Wow this is the best place to see the difference. waiting for more topics.

    ReplyDelete
  4. Nice place to see complete difference.

    ReplyDelete
  5. Very nice points .. Thanks a lot Admin :)

    ReplyDelete