In Java, an interface is a blueprint of a class. It is used to achieve abstraction and multiple inheritance. An interface specifies a set of methods that a class must implement but does not contain any implementation for the methods itself.
Key Features of Interfaces
- Abstract Methods Only:
- Interfaces contain abstract methods (methods without a body). Starting from Java 8, they can also have default and static methods.
- Multiple Inheritance:
- A class can implement multiple interfaces, which is Java’s way of supporting multiple inheritance.
- Constants:
- Variables declared in an interface are implicitly public, static, and final.
- No Constructors:
- Interfaces cannot have constructors as they cannot be instantiated.
Why Use Interfaces?
- Abstraction: Provides a contract for what a class should do without specifying how.
- Multiple Inheritance: Allows a class to inherit behavior from multiple sources.
- Loose Coupling: Encourages the design of loosely coupled systems.
Syntax of an Interface
Defining an Interface
interface InterfaceName {
// Abstract method
void method1();
// Default method (Java 8+)
default void method2() {
System.out.println(“Default method in interface”);
}
// Static method (Java 8+)
static void method3() {
System.out.println(“Static method in interface”);
}
// Public, static, and final variable
int CONSTANT = 100;
}
Implementing an Interface
A class implements an interface using the implements keyword.
class ClassName implements InterfaceName {
// Implement abstract method
public void method1() {
System.out.println(“Method1 implementation in class”);
}
}
Example 1: Basic Interface
interface Animal {
void makeSound(); // Abstract method
}
class Dog implements Animal {
public void makeSound() {
System.out.println(“Woof! Woof!”);
}
}
class Cat implements Animal {
public void makeSound() {
System.out.println(“Meow! Meow!”);
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.makeSound();
Animal cat = new Cat();
cat.makeSound();
}
}
Output:
Woof! Woof!
Meow! Meow!
Example 2: Interface with Multiple Implementation
interface Shape {
void draw();
double area();
}
class Circle implements Shape {
double radius;
Circle(double radius) {
this.radius = radius;
}
public void draw() {
System.out.println(“Drawing a circle”);
}
public double area() {
return Math.PI * radius * radius;
}
}
class Rectangle implements Shape {
double length, width;
Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public void draw() {
System.out.println(“Drawing a rectangle”);
}
public double area() {
return length * width;
}
}
public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5);
circle.draw();
System.out.println(“Area: ” + circle.area());
Shape rectangle = new Rectangle(4, 6);
rectangle.draw();
System.out.println(“Area: ” + rectangle.area());
}
}
Output:
Drawing a circle
Area: 78.53981633974483
Drawing a rectangle
Area: 24.0
Example 3: Multiple Inheritance Using Interfaces
interface Printable {
void print();
}
interface Showable {
void show();
}
class Document implements Printable, Showable {
public void print() {
System.out.println(“Printing document…”);
}
public void show() {
System.out.println(“Showing document…”);
}
}
public class Main {
public static void main(String[] args) {
Document doc = new Document();
doc.print();
doc.show();
}
}
Output:
Printing document…
Showing document…
Default and Static Methods in Interfaces
Introduced in Java 8, these methods allow interfaces to have some implemented methods.
Default Method
A method with a default implementation that can be overridden by implementing classes.
interface Vehicle {
default void start() {
System.out.println(“Starting vehicle…”);
}
}
class Car implements Vehicle {
public void start() {
System.out.println(“Car started!”);
}
}
Static Method
A method that belongs to the interface and cannot be overridden by implementing classes.
interface Vehicle {
static void clean() {
System.out.println(“Cleaning vehicle…”);
}
}
Characteristics of Interface Variables
- Public, Static, and Final: Variables are constants by default.
- Must be Initialized: Variables in an interface must be assigned a value when declared.
Example:
interface Constants {
int MAX = 100; // Public, static, and final
}
Differences Between Abstract Classes and Interfaces
Feature | Abstract Class | Interface |
Methods | Can have abstract and concrete methods. | Only abstract, default, and static methods. |
Inheritance | Supports single inheritance. | Supports multiple inheritance. |
Constructors | Can have constructors. | Cannot have constructors. |
Variables | Can have any type of variables. | Variables are public, static, and final by default. |
Access Modifiers | Methods can have any access modifier. | Methods are public by default. |
Advantages of Interfaces
- Facilitates multiple inheritance in Java.
- Encourages the use of abstraction and polymorphism.
- Improves code maintainability and flexibility.
Conclusion
Interfaces in Java provide a powerful mechanism for abstraction and enable multiple inheritance by defining a set of rules or contracts that implementing classes must follow. They are foundational to building modular, scalable, and reusable code in Java.