Similarities And Difference Between Abstract Class And Interface

Earlier we have learnt about interface in THIS POST and abstract class THIS POST. Now let's try to understand similarities difference between abstract class and interface in java software development language. Interviewer can ask you this question when you will go for selenium webdriver with java interview. There are few similarities and differences between interface and abstract class in java software development language as bellow.
Similarities
  • Interface can not be instantiated. Same way, you can not instantiate abstract class.
  • That means you can not create object of interface or abstract class.
Difference
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 interface 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.
Interface can not hold main method, static methods or constructor.Abstract class can hold main method, static methods or constructor.
Also it can hold only static and final variables and mandatory to initialize them.It can hold static, non static, final, non final variables and also it is not mandatory to initialize them.
We can achieve 100% abstraction using 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 concrete methods too.
When you add a new method in existing interface it breaks all its implementation and you need to provide an implementation in all clients which is not good.By using abstract class you can provide default implementation in super class by creating 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