What is the difference between Interface and Abstract Class?

  1. When there is only requirement specification provided and there is no clarity on the implementation, then we need to go for an interface to get the high level view of the requirement. This is 100% abstraction. When we know about the implementation partially, then we should go for abstract classes. Abstract classes in this level help the programmer to add the implementation in future.
  2. In interface every method is public and abstract by default, hence interface is pure abstract class. All the methods in abstract class need not to be public and abstract , hence concrete methods also can be used in abstract classes.
  3. The methods in interface can not be declared with the modifiers private, protected, final, static, synchronized, native, strictfp. Whereas, the methods in the abstract classed can be declared with any type of modifiers.
  4. Every variable present inside interface is public , static and final by default.Whereas, the variables inside the abstract class need not be public, static and final.
  5. The variables inside the interfaces cannot be declared with private, protected, transient, volatile. Whereas, the variables in abstract classes can be declared with any type of modifiers. As we cannot create object to an interface, serialization is not possible with interface.
  6. Interface variables should be initialized at the time of declaration, if not we will get a compilation error. For Abstract class variables there is no such kind of restriction. 
  7. We cannot declare instance block or static block inside an interface. Whereas, we can declare instance block and static block inside abstract class.
  8. We cannot declare constructor inside an instance as there is no instance variable in interface. In abstract class, we can declare a constructor.

No comments:

Post a Comment