What is an interface in Java and how is it different from a class

In Java, an interface is a type that is similar to a class but is purely abstract in nature. It defines a contract for the classes that implement it, specifying a set of methods that those classes must implement. Unlike classes, interfaces cannot be instantiated on their own, and they do not contain any implementation of methods.

Here are some key characteristics of interfaces in Java and how they differ from classes:

### 1. Definition:

- **Class:**
  - A class is a blueprint for creating objects. It can have fields (variables) and methods, and it provides a way to encapsulate data and behavior into a single unit.

- **Interface:**
  - An interface is a collection of abstract methods (methods without a body) and constants (public, static, and final fields). It defines a contract that classes must adhere to by implementing its methods.

### 2. Keyword:

- **Class:**
  - Declared using the `class` keyword.

- **Interface:**
  - Declared using the `interface` keyword.

### 3. Instantiation:

- **Class:**
  - Can be instantiated to create objects using the `new` keyword.

- **Interface:**
  - Cannot be instantiated directly. It is meant to be implemented by classes, and objects are instantiated from those classes.

### 4. Inheritance:

- **Class:**
  - Supports both single and multiple inheritance. A class can extend only one other class (single inheritance), but it can implement multiple interfaces (multiple inheritance).

- **Interface:**
  - Supports only multiple inheritance. A class can implement multiple interfaces.

### 5. Abstract Methods:

- **Class:**
  - Can have abstract methods (methods without a body) if declared as an abstract class. Abstract methods are meant to be implemented by subclasses.

- **Interface:**
  - Contains only abstract methods. All methods declared in an interface are implicitly abstract and public. Starting from Java 8, interfaces can have default methods (with a default implementation) and static methods.

### 6. Fields:

- **Class:**
  - Can have fields (variables), including regular variables and constants.

- **Interface:**
  - Can only have constants (public, static, and final fields).

### 7. Access Modifiers:

- **Class:**
  - Can have various access modifiers (public, private, protected, etc.) for methods and fields.

- **Interface:**
  - All methods declared in an interface are implicitly public, and fields are implicitly public, static, and final.

### 8. Constructors:

- **Class:**
  - Can have constructors, and they are called when an object is instantiated.

- **Interface:**
  - Cannot have constructors. Interfaces cannot be instantiated, and they do not have instance variables.

In summary, while classes in Java are used to define objects with both state and behavior, interfaces are used to define contracts that classes must adhere to by implementing the specified methods. Interfaces provide a way to achieve abstraction, polymorphism, and multiple inheritance in Java.

Tags