DEV Community

Cover image for Differences between abstract class and interface in Java
MANIKANDAN
MANIKANDAN

Posted on

Differences between abstract class and interface in Java

What is an Abstract Class?

Abstraction in Java is the process of hiding internal implementation details and showing only essential functionality to the user. It focuses on what an object does rather than how it does it.

What is an Interface?
Java interface is a reference type in Java, similar to a class, that is used to achieve abstraction and multiple inheritance. It defines a set of abstract methods that a class must implement. Interfaces specify what a class must do, but not how it does it. This makes code more modular, flexible, and easier to manage, especially in large applications.



--------------------------------------------------------------
|  Abstract Class             |       Interface              | 
--------------------------------------------------------------
|  Abstract keyword used to   | Interface keyword used to    |
|  declare the abstract class.| declare interface. Also,     |
|  Also, the abstract class   | interface can be implemented |
|  can be extended by using   | by using the keyword         |
|  the keyword “extends”      | “implements”                 |     
|------------------------------------------------------------|
|  Can have access modifier   |  Does not have access        |
|                             |  modifiers, and everything   |
|                             |  defined inside is assumed   |
|                             |  to be a public modifier     |
|-----------------------------|------------------------------|
| Can declare constructors    | Cannot declare constructors  |
| and destructors             | or destructors               |
|------------------------------------------------------------|
| Can have data fields        | Cannot have data fields      |
|-----------------------------|------------------------------|
| Can have any type of class  | Can only have public members,|
| member (e.g., public,       | by default                   |
| private, protected)         |                              |
|-----------------------------|------------------------------|
| Can have only one abstract  | Can implement several        |
| class                       |                              |
--------------------------------------------------------------





Enter fullscreen mode Exit fullscreen mode

Top comments (0)