What is an Interface in Java?
A Java Interface is a completely abstract class used to group related methods with empty bodies. It's a blueprint for classes.
Think of an interface as a contract: any class that implements it must provide concrete implementations of the methods declared in the interface.
Key Points
- An interface contains abstract methods (by default, public and abstract).
- A class implements an interface using the
implementskeyword. - A class can implement multiple interfaces (Java supports multiple inheritance using interfaces).
- From Java 8, interfaces can have
defaultandstaticmethods. - From Java 9, interfaces can also have
privatemethods.
Syntax
interface InterfaceName {
void method1(); // abstract method
void method2(); // abstract method
}
class ClassName implements InterfaceName {
public void method1() {
// implementation
}
public void method2() {
// implementation
}
}
Example 1: Simple Interface
// Interface
interface Animal {
void sound(); // abstract method
}
// Class implementing interface
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
}
// Main class
public class InterfaceExample {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // Output: Dog barks
}
}
Example 2: Multiple Interfaces
interface Printable {
void print();
}
interface Showable {
void show();
}
// Class implementing both interfaces
class Document implements Printable, Showable {
public void print() {
System.out.println("Printing document...");
}
public void show() {
System.out.println("Showing document...");
}
}
public class MultipleInterfaceExample {
public static void main(String[] args) {
Document doc = new Document();
doc.print();
doc.show();
}
}
Output:
Printing document...
Showing document...
Example 3: Interface with default and static Methods (Java 8+)
interface MyInterface {
void display(); // abstract method
default void greet() {
System.out.println("Hello from default method!");
}
static void help() {
System.out.println("This is a static method in interface.");
}
}
class MyClass implements MyInterface {
public void display() {
System.out.println("Implementing display method.");
}
}
public class InterfaceJava8Example {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.display();
obj.greet(); // calling default method
MyInterface.help(); // calling static method
}
}
Output:
Implementing display method.
Hello from default method!
This is a static method in interface.
Why Use Interfaces?
| Benefit | Explanation |
|---|---|
| Multiple Inheritance | Java doesn’t support multiple class inheritance, but allows multiple interfaces. |
| Loose Coupling | Promotes abstraction between components. |
| Flexibility & Scalability | You can change implementation without affecting the interface. |
| Polymorphism | Interface references can point to any class that implements them. |
0
likes
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
