WikiGalaxy

Personalize

Java Interface Basics

Definition:

An interface in Java 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 or constructors.

Purpose:

Interfaces are used to achieve abstraction and multiple inheritance in Java. They provide a way to define methods that must be implemented by any class that implements the interface.


interface Animal {
    void eat();
    void travel();
}
    

Key Points:

Interfaces can contain only abstract methods and final fields (constants). They do not have constructors and cannot be instantiated.

Implementing Interfaces

Implementation:

A class implements an interface by providing the body of all the methods declared in the interface.


class Mammal implements Animal {
    public void eat() {
        System.out.println("Mammal eats");
    }
    public void travel() {
        System.out.println("Mammal travels");
    }
}
    

Note:

If a class does not implement all methods of an interface, it must be declared abstract.

Multiple Interfaces Implementation

Multiple Inheritance:

A class can implement multiple interfaces, which allows for a form of multiple inheritance.


interface Animal { void eat(); }
interface Bird { void fly(); }
class Bat implements Animal, Bird {
    public void eat() { System.out.println("Bat eats"); }
    public void fly() { System.out.println("Bat flies"); }
}
    

Advantage:

This approach allows for greater flexibility and code reuse.

Default Methods in Interfaces

Default Methods:

Introduced in Java 8, default methods allow interfaces to have methods with a default implementation.


interface Animal {
    void eat();
    default void travel() {
        System.out.println("Animal travels");
    }
}
    

Use Case:

They provide backward compatibility to add new functionalities to interfaces without breaking existing implementations.

Static Methods in Interfaces

Static Methods:

Interfaces can also include static methods, which belong to the interface class and not to any instance.


interface Animal {
    static void info() {
        System.out.println("Animal interface");
    }
}
    

Access:

Static methods in interfaces can be called using the interface name.

Functional Interfaces

Concept:

A functional interface is an interface that contains exactly one abstract method. They are the foundation of lambda expressions in Java.


@FunctionalInterface
interface MyFunctionalInterface {
    void execute();
}
    

Lambda Expressions:

Functional interfaces are used extensively in lambda expressions and method references.

Marker Interfaces

Definition:

A marker interface is an interface with no methods or fields. It is used to convey metadata to the JVM or compiler.


interface Serializable {
}
    

Example:

The Serializable interface is a well-known marker interface in Java.

Nested Interfaces

Concept:

Interfaces can be nested within classes or other interfaces. This is useful for organizing code and defining interfaces that are only relevant within a specific context.


class OuterClass {
    interface NestedInterface {
        void nestedMethod();
    }
}
    

Use Case:

Nested interfaces are often used in event handling and callback mechanisms.

Interface vs Abstract Class

Comparison:

While both interfaces and abstract classes can be used to achieve abstraction, there are key differences between them. An abstract class can have instance variables and constructors, while an interface cannot.


abstract class AbstractAnimal {
    abstract void eat();
    void sleep() {
        System.out.println("Sleeping");
    }
}
interface Animal {
    void eat();
}
    

Conclusion:

Choose interfaces for defining capabilities and abstract classes for shared base functionality.

Interface Inheritance

Inheritance:

Interfaces can extend other interfaces, allowing for a hierarchy of interfaces to be created.


interface Animal {
    void eat();
}
interface Bird extends Animal {
    void fly();
}
    

Benefit:

This allows for a more organized and modular approach to interface design.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025