Skip to content

Java implements | Interface | keyword | Example code

  • by

Java Implements keyword is for implementing an interface. This keyword comes to solve Multiple Inheritance problems in java. If you want a inherit many classes by a single one, this is not possible with extends the class.  You need to define an Interface and then use Java Implements to inherit multiple Interface properties.

Java implements Interface keyword Example

In this tutorial we will learn about Java implements Interface with examples. But before the start, you must have basic knowledge about Interface, class, and Inheritance. Here is a link to it.

Implements: You are implementing a contract. The class implementing the interface “has a” capability.

Syntax

Here is the simple syntax of how to use implements.

public class MyMath implements Add {
    //your class code
}

Java implements an example

Here is an example of how java implements the interface. In the code, we created a one interface Add and class Hello inherit method by using implements.

interface Add {
    default int addMethod(int a, int b) {
        return a + b;
    }
}

class Hello implements Add{

    public static void main(String[] args) {
        Hello myMath = new Hello();
        int total = myMath.addMethod(2, 5);

        System.out.println("Addition of number is " + total);
    }

}

Output: Addition of number is 7

Question: How to Java implements multiple interfaces?

Answer: Same as the Above example we can do multiple interfaces implement in class. Below an example of inheriting methods from 2 Interfaces.

Note: A interface can have default methods in the interface, this is updated in Java 8 release.

interface Add {
    default int addMethod(int a, int b) {
        return a + b;
    }
}
interface Multiply extends Add {
    default int multMethod(int a, int b) {
        return a * b;
    }
}

class Hello implements Add, Multiply {

    public static void main(String[] args) {
        Hello myMath = new Hello();
        int total = myMath.addMethod(2, 5);
        int mult = myMath.multMethod(3, 4);

        System.out.println("Addition of number is " + total);
        System.out.println("Multiplication of number is " + mult);
    }

}

Output: Addition of number is 7
Multiplication of number is 12

Question: Can Java implements class?

Answer: no, Java can’t implement class they can extend the class.

For more about this topic must read the above-mentioned tutorial link. This is a very important chapter for coding and interview perspective. Do comment doubt and suggestions.

Do comment if you have any doubts and suggestions this topic.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java extends class is in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading