Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Object-Oriented Programming

What is Interface in Java

An interface is similar to a class but is declared using the interface keyword and contains only public static methods. It enables 100% abstraction.

Interfaces also support multiple inheritance, but we cannot create objects of an interface.

Every interface must have a subclass that implements all its abstract methods. If a class does not override these methods, it must be declared as abstract.

Unlike classes, interfaces do not inherit from a Object class and, therefore, do not have constructors. To inherit from another class or interface, we use the extends keyword, while to implement an interface, we use the implements keyword.

image (7).pngThe interface only contains public, static, final variables by default. and methods are implicitly public and abstract.

After Java 8, we can create Default methods and static methods as well inside the interface with the help of default and static keywords.

Default methods mean we can create the definition of the method also inside the interface. (We will learn more about default methods in the Java 8 article.) However, since Java 9, we can have private methods in an interface.

Syntax


public interface InterfaceName {
    
    // Constant variables (implicitly public, static, and final)
    int CONSTANT_VARIABLE = 100;
    
    // Abstract methods (implicitly public and abstract)
    void methodOne();
    int methodTwo(String param);

    // Default method (introduced in Java 8)
    default void defaultMethod() {
        System.out.println("This is a default method.");
    }

    // Static method (introduced in Java 8)
    static void staticMethod() {
        System.out.println("This is a static method.");
    }
}

Why Interface?

  • Interface helps to achieve 100% abstraction.

  • With the help of an interface, we can achieve multiple inheritance.

  • Interface can be used to achieve loose coupling.

Example


public interface Language {

   
    String LANGUAGE_NAME = "Java";

   
    void compile();

    void execute();

    
    default void showLanguageInfo() {
        System.out.println("Programming Language: " + LANGUAGE_NAME);
    }

   
    static void developerInfo() {
        System.out.println("Developed by: James Gosling");
    }
}

In the above example, we create the interface name as a language that contains the method as abstract compile() and execute(), the static method as developerInfo(), and the default method is showLanguageInfo().


public class JavaLanguage implements Language {

    @Override
    public void compile() {
        System.out.println("Compiling Java Code...");
    }

    @Override
    public void execute() {
        System.out.println("Executing Java Code...");
    }

    public static void main(String[] args) {
        JavaLanguage java = new JavaLanguage();
        
        // Calling abstract methods (implemented in class)
        java.compile();
        java.execute();
        
        // Calling default method from interface
        java.showLanguageInfo();
        
        // Calling static method from interface
        Language.developerInfo();
    }
}

Here, we create a class that implements the Language interface and overrides all its abstract methods. Inside the main method, we call these methods using an object of the subclass.

To call a static method, we use the class or interface name. Since developerInfo() it is a static method of the Language interface, we call it using the interface name.

Output

Compiling Java Code...
Executing Java Code...
Programming Language: Java
Developed by: James Gosling

Types of Interface

Normal Interface:

An interface that contains only abstract methods (before Java 8).

public interface NormalInterface {
    void methodOne();  // Abstract method
    void methodTwo();  // Abstract method
}

public class NormalClass implements NormalInterface {
    @Override
    public void methodOne() {
        System.out.println("Method One Implementation");
    }

    @Override
    public void methodTwo() {
        System.out.println("Method Two Implementation");
    }

    public static void main(String[] args) {
        NormalClass obj = new NormalClass();
        obj.methodOne();
        obj.methodTwo();
    }
}

Functional Interface:

An interface with only one abstract method (can have default & static methods).

Example: Runnable.

@FunctionalInterface
public interface FunctionalInterfaceExample {
    void singleMethod();  // Only one abstract method allowed

    // Default method
    default void defaultMethod() {
        System.out.println("Default method in functional interface");
    }
}

// Using Functional Interface with Lambda
public class FunctionalExample {
    public static void main(String[] args) {
        FunctionalInterfaceExample obj = () -> System.out.println("Lambda Expression for Functional Interface");
        obj.singleMethod();
        obj.defaultMethod();  // Calling default method
    }
}

Here, we created the example in future articles (Java 8). We will discuss how it works internally. and how we reduce the number of code lines with the help of a functional interface.

@FunctionalInterface restrict the interface to containing the only single abstract method.

Marker Interface:

It is an empty interface with no methods that is used as a tag. A marker interface is an empty interface (with no methods) used to signal or mark a class for special behavior. It acts as a tag that helps the JVM or frameworks recognize certain objects and apply specific logic.

Example: Serializable

public interface MarkerInterface {
    // No methods, acts as a tag
}

// Class implementing marker interface
public class MarkerClass implements MarkerInterface {
    public void display() {
        System.out.println("Marker Interface Example");
    }

    public static void main(String[] args) {
        MarkerClass obj = new MarkerClass();
        obj.display();
    }
}

Difference between interface and abstract class

Abstract Class

Interface

1. Abstract class is not 100% abstract

1. The interface is 100% abstract.

2. Here we can develop abstract and concrete methods,

2. In interface, we can only develop static default and abstract methods.

3. Abstract class inherited from object class

3. The interface doesn’t inherit from an abstract class.

4. Abstract class will have a constructor

4. The interface will not have the constructors.

5. Through abstract, we can not achieve multiple inheritance.

5. Through the interface, we can achieve multiple inheritance.

6. Variables inside the abstract class are declared as normal.

6. Variables inside the interface are declared automatically as private static and final.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board