Similarities And Difference Between Abstract Class And Interface

Earlier we learnt about interface in THIS POST and abstract class THIS POST. Now let's try to understand the similarities and difference between abstract class and interface in java software development language. The interviewer can ask you this question when you go for a Selenium webdriver with Java interview. There are a few similarities and differences between interface and abstract class in Java software development language as below.
Similarities between abstract class and interface

Here are a few similarities between interface and abstract class.

  • The interface can not be instantiated. Same way, you can not instantiate abstract class.
  • That means you can not create an object of interface or abstract class.
  • Interfaces and Abstract Classes can not be implemented.
Difference between abstract class and interface
Differences between interface and abstract class in java software development language are as bellow.

Interface
Abstract Class
We can use interface keyword to declare interface.We can use abstract keyword to declare abstract class.
Interface can hold only abstract methods(without implementation).Abstract class can hold abstract(without implementation) as well as non abstract methods.
Interface can be implemented using implements keyword.Abstract class can be extended using extends keyword.
We can achieve multiple inheritance using interfaces as we can implement multiple interfaces to any class.Abstract class doesn't support multiple inheritance as we can not extend more than one class.
The interface can not hold the main method, static methods, or constructor.An abstract class can hold the main method, static methods, or constructor.
Also, it can hold only static and final variables and is mandatory to initialize them.It can hold static, nonstatic, final, non final variables and also it is not mandatory to initialize them.
We can achieve 100% abstraction using the interface as all methods are abstract by default. It can not hold concrete methods.We can achieve partial(0% to 100%) abstraction using abstract class as it can hold abstract as well as concrete methods.
When you add a new method to the existing interface it breaks all its implementation and you need to provide an implementation for all clients which is not good.By using an abstract class you can provide a default implementation in a superclass by creating a concrete method. It is not required to provide its implementation in sub class.

These are the difference between abstract class and interface in java software development language which can helps you to choose abstract class or interface.

2 comments:

  1. Interface can hold static methods bro try doing this
    interface abc
    {
    static void display()
    {
    sop("hi");
    }
    }
    class Test implements abc
    {
    abc.display();
    }

    ReplyDelete
    Replies
    1. in interface only use abstract method with no implementation......
      so its wrong because you implements display() function....only define then do ...

      its declare with all mentods in implement's class

      Delete