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
implements
keyword. - A class can implement multiple interfaces (Java supports multiple inheritance using interfaces).
- From Java 8, interfaces can have
default
andstatic
methods. - From Java 9, interfaces can also have
private
methods.
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. |
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.