An interface in Java is a reference type that can contain only abstract methods (until Java 8, after which it can also have default and static methods). An interface provides a contract for classes to implement; it defines what a class should do, but not how it should do it. Any class that implements the interface must provide concrete implementations for all the methods declared in the interface.
Characteristics of an Interface
- It can only contain abstract methods (until Java 8).
- It can contain default methods (since Java 8), which have a body and can be inherited by implementing classes.
- A class can implement multiple interfaces (Java supports multiple inheritance through interfaces).
- An interface cannot have instance variables, but it can have constants (public static final variables).
Syntax:
interface InterfaceName {
// abstract method (no body)
returnType methodName();
// default method (Java 8 onwards)
default void defaultMethod() {
// Default method implementation
}
// static method (Java 8 onwards)
static void staticMethod() {
// Static method implementation
}
}
Example: Basic Example of Interface
//Main.java file
// Interface definition
interface Animal {
// Abstract method (does not have a body)
void makeSound();
// Default method
default void eat() {
System.out.println("This animal eats food.");
}
}
// Concrete class Dog implements Animal interface
class Dog implements Animal {
// Providing implementation for the abstract method makeSound()
public void makeSound() {
System.out.println("Woof! Woof!");
}
}
// Concrete class Cat implements Animal interface
class Cat implements Animal {
// Providing implementation for the abstract method makeSound()
public void makeSound() {
System.out.println("Meow! Meow!");
}
}
// Main class to test the interface and its implementation
public class Main {
public static void main(String[] args) {
// Creating objects of Dog and Cat
Animal myDog = new Dog();
Animal myCat = new Cat();
// Calling the methods
myDog.makeSound(); // Dog's sound
myDog.eat(); // Dog's eat (default method)
myCat.makeSound(); // Cat's sound
myCat.eat(); // Cat's eat (default method)
}
}
Explanation:
- Animal is an interface with an abstract method makeSound() and a default method eat().
- Dog and Cat both implement the Animal interface and provide their own implementations of the abstract method makeSound().
- Both classes also inherit the eat() method from the interface, as it’s a default method.
Interface with Static Methods
Interfaces can also contain static methods. Static methods in an interface are not inherited by implementing classes but can be accessed via the interface name.
Example:
//Main.java file
// Interface definition
interface Animal {
// Abstract method (does not have a body)
void makeSound();
// static method sleep
static void sleep() {
System.out.println("Animal sleeps.");
}
}
// Concrete class Dog implements Animal interface
class Dog implements Animal {
// Providing implementation for the abstract method makeSound()
public void makeSound() {
System.out.println("Woof! Woof!");
}
}
// Main class to test the interface and its implementation
public class Main {
public static void main(String[] args) {
// Creating objects of Dog
Dog myDog = new Dog();
// Calling the methods
myDog.makeSound(); // Dog's sound
Animal.sleep(); // call static method
}
}
Explanation:
- The Animal interface defines a static method sleep() which can be called directly from the interface.
- The Dog class implements the Animal interface and provides an implementation for the abstract method makeSound().
- The static method sleep() is called using the interface name Animal.
Output:
Animal sleeps.
Multiple Interfaces Implementation
Java allows a class to implement multiple interfaces, which is a way to achieve multiple inheritance.
Example:
//Main.java file
// Interface definition
interface Voice {
// Abstract method (does not have a body)
void makeSound();
}
interface Runnable {
// Abstract method (does not have a body)
void run();
}
// Concrete class Dog implements Animal interface
class Dog implements Voice, Runnable {
// Providing implementation for the abstract method makeSound() and run()
public void makeSound() {
System.out.println("Woof! Woof!");
}
public void run() {
System.out.println("Dog runs fast.");
}
}
// Main class to test the interface and its implementation
public class Main {
public static void main(String[] args) {
// Creating objects of Dog
Dog myDog = new Dog();
// Calling the methods
myDog.makeSound(); // Dog's sound
myDog.run(); // Dog's run
}
}
Explanation:
- Voice and Runnable are two interfaces.
- Dog implements both interfaces, meaning it must provide implementations for both makeSound() and run() methods.
- This demonstrates Java’s ability to support multiple inheritance through interfaces.
Output:
Dog runs fast.
Advantages of Using Interfaces
Multiple Inheritance: A class can implement multiple interfaces, overcoming the limitations of single inheritance in Java.
Loose Coupling: Interfaces promote loose coupling between classes. Classes can interact with each other based on interfaces rather than specific class types.
Flexibility: Interfaces allow you to define methods that can be implemented in any class, providing flexibility in the design of your program.
Polymorphism: Interfaces are often used in polymorphic scenarios, where you can treat different classes uniformly by referring to them via their common interface.
When to Use Interfaces
1. When you want to define a contract that multiple classes can implement.
2. When you need a class to support multiple types (since Java does not support multiple class inheritance).
3. When you want to define common behavior for unrelated classes.