Interface in JavaLast Updated : 3 Jan 2026 An interface in Java is used to define a common set of methods that a class must implement. An interface helps in achieving abstraction and supports multiple inheritance. In this chapter, we will learn how to define an interface, how to achieve abstraction and multiple inheritance using interfaces. What is Interface in Java?An interface is a blueprint of a class that contains static constants and abstract methods. Interfaces are used to achieve abstraction. An interface contains only abstract methods (methods without a body) and variables. It cannot be instantiated, similar to an abstract class, and represents an IS-A relationship. Since Java 8, interfaces can include default and static methods. Since Java 9, interfaces can also contain private methods. Why Use an Interface in Java?An interface is used in Java for the following reasons:
Declaring an InterfaceAn interface is declared using the interface keyword. It provides complete abstraction, which means all methods are abstract by default, and all fields are public, static, and final. A class that implements an interface must provide implementations for all the methods declared in the interface. Syntax of Interface DeclarationHere is the syntax to declare an interface: Example of Declaring an InterfaceHere is an example to declare an interface: In this example, the Animal interface declares two methods: eat() and sleep(). Any class that implements the Animal interface must provide concrete implementations for these methods. Implicit Modifiers Added by the Java CompilerIn a Java interface, the compiler automatically adds certain modifiers to its members. All methods are implicitly declared as public and abstract, even if these keywords are not explicitly written. Similarly, all fields declared in an interface are implicitly public, static, and final. In other words, interface methods are public and abstract by default, while interface fields are public, static, and final by default. Since Java 8, interfaces can include default and static methods, which are discussed later in this chapter. Relationship Between Classes and InterfacesAs shown in the following figure, a class extends another class, an interface extends another interface, and a class implements an interface. This relationship defines how classes and interfaces interact and support inheritance and abstraction in Java. ![]() Interface ExamplesPractice the following examples to understand how interfaces are declared, implemented, and used in Java. Example 1: Printable InterfaceThe following example shows an interface with a single method. The implementation of the interface is provided by a class. ExampleCompile and RunOutput: Hello Example 2: Drawable InterfaceThe following example demonstrates how multiple classes can implement the same interface. In a real scenario, the interface is usually defined by one party, while different classes provide their own implementations. The implementation details remain hidden from the user. ExampleCompile and RunOutput: drawing circle Example 3: Bank InterfaceThis example shows how an interface can be used to provide a common structure for related classes. Each bank class provides its own implementation of the interface method. ExampleCompile and RunOutput: SBI ROI: 9.15 PNB ROI: 9.7 HDFC ROI: 8.7 Implementing Multiple Inheritance Using InterfacesWhen a class implements multiple interfaces, or an interface extends more than one interface, it is known as multiple inheritance. In other words, multiple inheritance is implemented by allowing a class to implement multiple interfaces or an interface to extend multiple interfaces. ![]() ExampleThe following example demonstrates how a class can implement multiple interfaces to achieve multiple inheritance. Output: printing data... showing data... Inheritance of InterfacesA class implements an interface, whereas an interface can extend another interface. When one interface extends another, it inherits all the abstract methods of the parent interface. A class that implements the child interface must provide implementations for all methods declared in both the parent and child interfaces. ExampleThe following example demonstrates how one interface can inherit another interface and how a class implements the child interface by providing implementations for all inherited methods. Output: Hello Welcome Interfaces and PolymorphismOne of the main benefits of interfaces is that they support polymorphism. An interface reference can point to any object of a class that implements it. This allows the program to decide at runtime which method implementation to use. ExampleThe following example demonstrates how an interface reference can point to an object of a class that implements it, showing polymorphic behavior. Output: Dog is eating Dog is sleeping In this example, myAnimal is an interface reference variable of type Animal, referring to a Dog object. It demonstrates polymorphic behavior, where the method calls are resolved at runtime based on the actual object type. Java 8 Default Method in InterfaceSince Java 8, we can have method body in interface. But we need to make it default method. Let's see an example: ExampleCompile and RunOutput: drawing rectangle default method Java 8 Static Method in InterfaceSince Java 8, we can have static methods in the interface. Let's see an example: ExampleCompile and RunOutput: drawing rectangle 27 Nested InterfaceAn interface can be declared inside another interface. Such interfaces are called nested interfaces. A class can implement the outer interface, the nested interface, or both, depending on the requirement. ExampleThe following example demonstrates how to declare and implement a nested interface: Output: Printing document... Printing message... Interface Vs. Abstract ClassAn abstract class can have both abstract and concrete methods, constructors, and different types of variables, but it does not support multiple inheritance. An interface can only declare abstract methods (with default and static methods allowed since Java 8), has no constructors, and supports multiple inheritance. Read more about the differences between abstract class and interfaces here: Java - Abstract Class Vs. Interfaces |
We request you to subscribe our newsletter for upcoming updates.