CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Interface in Java: Mastering Abstraction and Multiple Inheritance with Examples

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Interface in Java: Mastering Abstraction and Multiple Inheritance with Examples

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 4 min read · 0 Comments
Share: in X

Interfaces in Java are the backbone of abstraction and multiple inheritance. They define a contract that classes must follow, enabling loosely coupled, modular code. With Java 8+ features like default and static methods, interfaces have become even more powerful. In this guide, you’ll learn how to declare interfaces, implement them effectively, and use them to build scalable systems.

Table of Contents

  • What is an Interface in Java?
    • Real-World Example:
  • Syntax of an Interface
  • How to Implement an Interface
    • Step 1: Define the Interface
  • Key Features of Java Interfaces
    • 1. Default Methods (Java 8+)
    • 2. Static Methods (Java 8+)
    • 3. Multiple Inheritance
  • Interface vs. Abstract Class
  • Common Mistakes (and Fixes)
    • 1. Forgetting to Implement Methods
    • 2. Default Method Conflicts
    • 3. Incorrect Access Modifiers
  • Best Practices for Interfaces
  • Conclusion
  • FAQs About Java Interfaces
    • Can an interface extend another interface?
    • Can an interface have variables?
    • Can a class implement multiple interfaces with the same method?
    • When to use an interface vs. an abstract class?

What is an Interface in Java?

An interface is a reference type that:

  1. Contains abstract methods (no implementation) and constants (public static final fields).
  2. Can include default methods (with implementation) and static methods (Java 8+).
  3. Enables multiple inheritance (a class can implement multiple interfaces).

Real-World Example:

Think of an interface as a power socket. Any device (class) that follows the socket’s design (interface) can plug in, regardless of its internal workings.

Syntax of an Interface

interface Vehicle {  
    // Constant (implicitly public static final)  
    String TYPE = "Transport";  

    // Abstract method (implicitly public abstract)  
    void start();  

    // Default method (Java 8+)  
    default void honk() {  
        System.out.println("Beep!");  
    }  

    // Static method (Java 8+)  
    static void displayType() {  
        System.out.println("Type: " + TYPE);  
    }  
}  

How to Implement an Interface

Step 1: Define the Interface

interface Drawable {  
    void draw();  
}  

Step 2: Implement the Interface

class Circle implements Drawable {  
    @Override  
    public void draw() {  
        System.out.println("Drawing a circle");  
    }  
}  

class Rectangle implements Drawable {  
    @Override  
    public void draw() {  
        System.out.println("Drawing a rectangle");  
    }  
}  

Step 3: Use the Implementations

public class Main {  
    public static void main(String[] args) {  
        Drawable circle = new Circle();  
        Drawable rect = new Rectangle();  

        circle.draw(); // Output: Drawing a circle  
        rect.draw();   // Output: Drawing a rectangle  
    }  
}  

Key Features of Java Interfaces

1. Default Methods (Java 8+)

Add methods to interfaces without breaking existing implementations:

interface Payment {  
    void pay(double amount);  

    default void printReceipt() {  
        System.out.println("Payment successful!");  
    }  
}  

class CreditCard implements Payment {  
    @Override  
    public void pay(double amount) {  
        System.out.println("Paid via credit card: $" + amount);  
    }  
}  

2. Static Methods (Java 8+)

Define utility methods within interfaces:

interface MathUtils {  
    static double square(double num) {  
        return num * num;  
    }  
}  

// Usage:  
double result = MathUtils.square(5); // 25.0  

3. Multiple Inheritance

A class can implement multiple interfaces:

interface Flyable { void fly(); }  
interface Swimmable { void swim(); }  

class Duck implements Flyable, Swimmable {  
    @Override  
    public void fly() { System.out.println("Duck flying"); }  

    @Override  
    public void swim() { System.out.println("Duck swimming"); }  
}  

Interface vs. Abstract Class

InterfaceAbstract Class
Supports multiple inheritance.Single inheritance only.
All methods are public abstract by default.Can have abstract + concrete methods.
Fields are public static final.Can have instance fields.
No constructors.Can have constructors.
Use for unrelated classes sharing behavior.Use for related classes sharing code.

Common Mistakes (and Fixes)

1. Forgetting to Implement Methods

Error:

class Car implements Vehicle { } // Error: Missing start()  

Fix: Implement all abstract methods or declare the class abstract.

2. Default Method Conflicts

Error:

interface A { default void print() { ... } }  
interface B { default void print() { ... } }  
class C implements A, B { } // Error: Duplicate default methods  

Fix: Override the method in the class:

class C implements A, B {  
    @Override  
    public void print() {  
        A.super.print(); // Either Choose one implementation  from interface or write your own implementation logic here
    }  
}  

3. Incorrect Access Modifiers

Error:

interface Logger {  
    private void log(); // Error: Interface methods are public  
}  

Fix: Omit access modifiers (methods are public by default).

Best Practices for Interfaces

  1. Name Interfaces as Adjectives: Use -able suffixes (e.g., Runnable, Serializable).
  2. Keep Interfaces Focused: Follow the Single Responsibility Principle.
  3. Use Default Methods Sparingly: Avoid cluttering interfaces with complex logic.
  4. Combine with Abstract Classes: Use abstract classes for shared code and interfaces for contracts.

Conclusion

Interfaces in Java unlock the power of abstraction and multiple inheritance, enabling flexible, future-proof code. By mastering default methods, static methods, and clear contracts, you’ll design systems that adapt to changing requirements with ease.

FAQs About Java Interfaces

Can an interface extend another interface?

Yes! Use extends:
interface A { }
interface B extends A { }

Can an interface have variables?

Only public static final constants.

Can a class implement multiple interfaces with the same method?

Yes, if the method signature is identical.

When to use an interface vs. an abstract class?

Use interfaces for multiple inheritance and defining contracts.
Use abstract classes to share code among related classes.

Related Posts:

  • Abstract Class in Java: Bridging Code Reusability…
  • Abstraction in Java
  • Inheritance in Java: A Developer’s Guide to Code Reusability
  • Control Statements in Java
  • Identifiers in Java
  • Packages in Java: A Guide to Modular, Maintainable Code
Tags:javalanguage-fundamentals
Was this article helpful?
← Previous ArticleBorderLayout in Java Swing
Next Article →Clean up space in the tableau server

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap