codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

36 / 65 topics
32Java Inheritance33Java Polymorphism34Java Inner Classes35Java Abstraction36Java Interface37Java Enum
Tutorials/Java Programming/Java Interface
☕Java Programming

Java Interface

Updated 2026-05-12
30 min read

Java Interface

In Java, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields. The methods in interfaces are abstract by default.

Interfaces play a crucial role in defining a contract for classes without dictating how they should implement the behavior. This makes them ideal for achieving abstraction and promoting polymorphism.

Introduction

Understanding Java interfaces is essential for building flexible and maintainable code. They allow you to define a set of methods that a class must implement, ensuring that all implementing classes adhere to a common protocol. In this tutorial, we will explore how interfaces work in Java, the implements keyword, and how multiple interfaces can be implemented by a single class.

Core Content

What is an Interface?

An interface in Java is defined using the interface keyword. It can contain method signatures (abstract methods) that must be implemented by any class that implements the interface. Interfaces are used to specify a set of methods that related classes should implement, promoting code reusability and flexibility.

Example 1: Basic Interface

Java
1// Animal.java
2public interface Animal {
3 void makeSound();
4};

In this example, we define an Animal interface with a single method makeSound(). Any class that implements this interface must provide an implementation for the makeSound() method.

Implementing an Interface

To implement an interface in Java, use the implements keyword followed by the interface name. The implementing class must provide concrete implementations for all the abstract methods defined in the interface.

Example 2: Implementing a Single Interface

Java
1// Dog.java
2public class Dog implements Animal {
3 @Override
4 public void makeSound() {
5 System.out.println("Woof!");
6 }
7}

In this example, the Dog class implements the Animal interface and provides an implementation for the makeSound() method.

Multiple Interfaces

A class can implement multiple interfaces. This is useful when a class needs to inherit behavior from more than one source. The implementing class must provide concrete implementations for all methods defined in each of the interfaces.

Example 3: Implementing Multiple Interfaces

Java
1// Flyable.java
2public interface Flyable {
3 void fly();
4}
5
6// Bird.java
7public class Bird implements Animal, Flyable {
8 @Override
9 public void makeSound() {
10 System.out.println("Chirp!");
11 }
12
13 @Override
14 public void fly() {
15 System.out.println("Flying high in the sky.");
16 }
17}

In this example, the Bird class implements both the Animal and Flyable interfaces. It provides implementations for all methods defined in these interfaces.

Default Methods

Java 8 introduced default methods in interfaces, allowing interface authors to add new methods without breaking existing implementing classes. Default methods have a body and are marked with the default keyword.

Example 4: Using Default Method

Java
1// Animal.java
2public interface Animal {
3 void makeSound();
4
5 default void eat() {
6 System.out.println("Eating...");
7 }
8}
9
10// Dog.java
11public class Dog implements Animal {
12 @Override
13 public void makeSound() {
14 System.out.println("Woof!");
15 }
16
17 // Optional to override the default method
18 @Override
19 public void eat() {
20 System.out.println("Dog is eating.");
21 }
22}

In this example, the Animal interface includes a default method eat(). The Dog class can choose to override this method or use the default implementation.

Static Methods

Java 8 also introduced static methods in interfaces. These methods belong to the interface itself and cannot be overridden by implementing classes.

Example 5: Using Static Method

Java
1// Animal.java
2public interface Animal {
3 void makeSound();
4
5 static void breathe() {
6 System.out.println("Breathing...");
7 }
8}
9
10// Dog.java
11public class Dog implements Animal {
12 @Override
13 public void makeSound() {
14 System.out.println("Woof!");
15 }
16
17 public static void main(String[] args) {
18 Animal.breathe(); // Calling the static method from the interface
19 }
20}

In this example, the Animal interface includes a static method breathe(). The Dog class can call this method using the interface name.

Practical Example

Let's create a practical example that demonstrates interfaces, multiple inheritance, and polymorphism. We'll define an interface for a vehicle and two implementing classes: Car and Bike.

Java
1// Vehicle.java
2public interface Vehicle {
3 void start();
4 void stop();
5}
6
7// Car.java
8public class Car implements Vehicle {
9 @Override
10 public void start() {
11 System.out.println("Car is starting.");
12 }
13
14 @Override
15 public void stop() {
16 System.out.println("Car is stopping.");
17 }
18}
19
20// Bike.java
21public class Bike implements Vehicle {
22 @Override
23 public void start() {
24 System.out.println("Bike is starting.");
25 }
26
27 @Override
28 public void stop() {
29 System.out.println("Bike is stopping.");
30 }
31}
32
33// Main.java
34public class Main {
35 public static void main(String[] args) {
36 Vehicle car = new Car();
37 Vehicle bike = new Bike();
38
39 car.start();
40 car.stop();
41
42 bike.start();
43 bike.stop();
44 }
45}

In this example, we define a Vehicle interface with two methods: start() and stop(). The Car and Bike classes implement the Vehicle interface and provide their own implementations of these methods. In the Main class, we create instances of Car and Bike, assign them to Vehicle references, and call the start() and stop() methods.

Summary

  • Interfaces define a contract for classes without dictating how they should implement the behavior.
  • Use the implements keyword to implement an interface in a class.
  • A class can implement multiple interfaces, promoting code reuse.
  • Default methods allow adding new methods to interfaces without breaking existing implementations.
  • Static methods belong to the interface and cannot be overridden by implementing classes.

What's Next?

In the next topic, we will explore Java enums. Enums provide a way to define a set of constants with associated values, making your code more readable and maintainable. Enums are particularly useful for representing fixed sets of related constants, such as days of the week or states in a system.

Stay tuned for the next tutorial on Java enums!


PreviousJava AbstractionNext Java Enum

Recommended Gear

Java AbstractionJava Enum