1/17/15

Difference between abstract class and interface

Both Abstract Class and Interface are used to implement abstraction in Java. But they are different in application and nature. Some key difference between Abstract Class and Interface is given below

Abstract Class

Abstract class  - Class declared with abstract keyword.
Abstract method - A method that is declared without implementation.
You can have an abstract class without having abstract method. But that is not vice versa as if a class has abstract method then it must declared as abstract.
You make abstract class if you want to your subclass to implement certain methods declared by you. Additional benefit with abstract class is that you can also give definitions in abstract class. Purpose of abstract class is to implement Modeling.
  1. We can say abstract classes are not 100% or <100% Abstract in nature.
  2. We can have concrete method in Abstract class.
  3. Any java class will extend this class.
  4. It can extend other abstract class and implement other interfaces.
  5. It can have private methods.

Interface

You should use Interface if you are creating Model classes and you want to force your user(who sub-classes your class). You also achieve abstraction with Interfaces. Variables declared in interfaces are public static final. They can work as global constants.
  1. While we can say interface is 100% Abstract in nature.
  2. We can not have concrete method(implementations) in Abstract class.
  3. Any Java class will implement this.
  4. It can not extend other classes only can implement other interfaces.
  5. it can not have private methods.