Implements Keyword in Java

Implements keyword

Which keyword is used by a class to use an interface defined previously?

Implements keyword in Java, the keyword “implements” is used by a class to indicate that it is implementing an interface that has been defined previously. By implementing an interface, a class agrees to provide an implementation for all the methods declared in that interface.

Here’s an example to illustrate this:

Implements Keyword example 1:

// Define an interface
interface MyInterface {
    void myMethod();
}

// Implement the interface in a class
class MyClass implements MyInterface {
    // Implementing the method from the interface
    public void myMethod() {
        System.out.println("Implementation of myMethod");
    }
}

// Create an instance of the class
MyClass obj = new MyClass();

// Call the method defined in the interface
obj.myMethod();

In the above example, we have an interface named MyInterface with a single method myMethod(). Then, we have a class named MyClass that implements the MyInterface interface using the implements keyword. The class provides the implementation for the myMethod() method.

Finally, we create an instance of the MyClass and call the myMethod() method. Since MyClass implements MyInterface, it is required to provide an implementation for the myMethod() method.


Here are more examples and details to further explain the concept of using the “implements” keyword to implement an interface in Java:

  1. Implementing Multiple Interfaces:
    A class in Java can implement multiple interfaces by separating them with commas. This allows the class to inherit and provide implementations for all the methods declared in each interface.
interface Interface1 {
    void method1();
}

interface Interface2 {
    void method2();
}

class MyClass implements Interface1, Interface2 {
    public void method1() {
        System.out.println("Implementation of method1");
    }

    public void method2() {
        System.out.println("Implementation of method2");
    }
}

MyClass obj = new MyClass();
obj.method1(); // Output: Implementation of method1
obj.method2(); // Output: Implementation of method2

In this example, the class MyClass implements both Interface1 and Interface2. It provides the implementation for the methods method1() and method2() declared in each interface.

  1. Inheriting Default Methods:
    Interfaces in Java can also have default methods, which are methods with an implementation defined within the interface itself. When a class implements an interface, it can inherit and optionally override the default methods of the interface.
interface Interface3 {
    default void method3() {
        System.out.println("Default implementation of method3");
    }
}

class MyClass2 implements Interface3 {
    // No need to provide an implementation for method3()
}

MyClass2 obj2 = new MyClass2();
obj2.method3(); // Output: Default implementation of method3

In this example, the interface Interface3 has a default method method3(). The class MyClass2 implements Interface3 but does not provide its own implementation for method3(). As a result, it inherits and uses the default implementation provided by the interface.

  1. Using Interface Reference:
    Another important aspect of interfaces is that they can be used as reference types. This means that an interface can be used to refer to objects of any class that implements that interface.
interface Vehicle {
    void start();
}

class Car implements Vehicle {
    public void start() {
        System.out.println("Car started");
    }
}

class Bike implements Vehicle {
    public void start() {
        System.out.println("Bike started");
    }
}

Vehicle vehicle1 = new Car();
Vehicle vehicle2 = new Bike();

vehicle1.start(); // Output: Car started
vehicle2.start(); // Output: Bike started

In this example, the interface Vehicle defines a method start(). The classes Car and Bike both implement the Vehicle interface and provide their own implementation for the start() method. By using the Vehicle interface as the reference type, we can create objects of different classes and call the start() method, which results in different behaviors depending on the actual implementation provided by each class.

Overall, the “implements” keyword in Java allows a class to inherit and provide implementations for the methods declared in an interface. It enables the concept of interfaces and facilitates the implementation of contracts and polymorphism in Java programming.

Leave a Comment