
We can achieve abstraction using both interface and abstract class where we declare abstract methods. Further we will discuss the difference between Interface and Abstract class and basic concept of abstract method.
Table of Contents
Abstract Class
Before abstract class, let’s know about the abstract method in java. Abstract method is incomplete method which contains only the method header, doesn’t contain body,
Abstract class is a class that contains 0 or more abstract methods. In addition to abstract methods, it contains instance variables and concrete methods. The most important thing is you cannot create an object of the abstract class.
To use the abstract class first we create a subclass of this class and then we create a reference of abstract class.
Interface
Interface contains only the abstract methods and it is not possible to create an object of interface. But we can create separate classes where we implement al the methods of the interface.
All the methods of the interface are public and abstract by default.
Interface Vs Abstract class
There are following differences between Interface and Abstract class:
# | Interface | Abstract Class |
---|---|---|
1 | If we don’t know anything about implementation but just we have requirement specification we should go for interface. | If we are taking about implementation but not completely then we should go for abstract class. |
2 | The interface keyword is used to declare interface. | The abstract keyword is used to declare abstract class. |
3 | An interface can extend another Java interface only. | An abstract class can extend another Java class and implement multiple Java interfaces. |
4 | Variables declared in a Java interface are by default final. | But an abstract class may contain non-final variables. |
5 | The interface has only static and final variables. | Abstract class can have final, non-final, static and non-static variables. |
6 | We can’t declare interface method with private, protected, final, static, synchronized, native, strictfp. | There is no restriction on abstract class method modifiers. |
7 | We can’t declare interface variable with private, protected, transient, volatile. | There is no restriction on abstract class variable modifiers. |
8 | For interface variables compulsory we should perform initialization at the time of declaration otherwise we will get compile time error. | Not required to perform initialization at the time of declaration in case of abstract class. |
9 | Inside the interface we can’t declare instance and static block otherwise we will get compile time error. | Inside the abstract class we can’t declare instance and static block. |
10 | You cannot declare any constructor inside the interface. | Here we can declare constructor inside the abstract class, which will be executed at the time of child object creation. |
Summary of this page
Above you learned about the difference between abstract class and interface along with the basic idea about abstract method. This is the most asked topic in interface for programmers because in every application the interface and abstract class are used.