VOOZH about

URL: https://www.geeksforgeeks.org/java/interfaces-in-java/

⇱ Java Interface - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Interface

Last Updated : 16 Jun, 2026

An interface in Java is a blueprint that defines a set of methods a class must implement without providing full implementation details. It helps achieve abstraction by focusing on what a class should do rather than how it does it. Interfaces also support multiple inheritance in Java.

  • A class must implement all abstract methods of an interface.
  • All variables in an interface are public, static, and final by default.
  • Interfaces can have default, static, and private methods (Java 8+ and 9+).

Example: Defines constants and abstract methods, which are implemented by a class.


Output
Geek
10

Notes:

  • Private methods can only be called inside default or static methods.
  • Static methods are accessed using the interface name, not via objects.
  • To implement an interface, use the implements keyword.

Relationship Between Class and Interface

A class can extend another class and similarly, an interface can extend another interface. However, only a class can implement an interface and the reverse (an interface implementing a class) is not allowed.

πŸ‘ Relationship between Class and Interface

Use case of Class:

  • Use a class when you need to represent a real-world entity with attributes (fields) and behaviors (methods).
  • Use a class when you need to create objects that hold state and perform actions
  • Classes are used for defining templates for objects with specific functionality and properties.

Use case of Interface:

  • Use an interface when you need to define a contract for behavior that multiple classes can implement.
  • Interface is ideal for achieving abstraction and multiple inheritance.

Implementation: To implement an interface, we use the keyword implements

Vehicles like bicycles, cars, and bikes share common functionalities that can be defined in an interface. Each class implements these in its own way, ensuring reusability, scalability, and consistency.


Output
Bicycle present state : speed: 2 gear: 2
Bike present state : speed: 1 gear: 1

Multiple Inheritance in Java Using Interface

Java does not support multiple inheritance with classes to avoid ambiguity, but it supports multiple inheritance using interfaces.

πŸ‘ Intefaces-in-Java-2-


Output
Addition : 3
Substraction : 1

New Features Added in Interfaces in JDK 8

There are certain features added to Interfaces in JDK 8 update mentioned below:

1. Default Methods

  • Interfaces can define methods with default implementations.
  • Useful for adding new methods to interfaces without breaking existing implementations.

Output
hello

2. Static Methods

  • Interfaces can now include static methods.
  • These methods are called directly using the interface name and are not inherited by implementing classes.

Another feature that was added in JDK 8 is that we can now define static methods in interfaces that can be called independently without an object. These methods are not inherited.


Output
hello

3. Functional Interface

  • Functional interfaces can be used with lambda expressions or method references.
  • The @FunctionalInterface annotation can be used to indicate that an interface is a functional interface, although it’s optional.

New Features Added in Interfaces in JDK 9

From Java 9 onwards, interfaces can contain the following also:

1. Private Methods

  • Interface can now include private methods.
  • Private methods are defined within the interface but it cannot be accessed by the implementing classes.
  • Private methods cannot be overridden by implementing classes as they are not inherited.

Output
Engine started.
Vehicle is now driving.

Extending Interfaces

One interface can inherit another by the use of keyword extends. When a class implements an interface that inherits another interface, it must provide an implementation for all methods required by the interface inheritance chain.


Output
Method 1
Method 2
Method 3

Related Article

Class vs Interfaces

Comment
Article Tags:
Article Tags: