User Milan Vucic
Milan Vucic
Programming Tutor at Codementor.io

Implements in Java

Published in the Java Developer group
When some talk about the Java OOP paradigm of inheritance, they usually mean extending a parent class with an inheritor, a child class. However when you come across the Java implements keyword, it means that we move to another level of abstraction and start working with Interfaces in Java. We are going to talk about what interfaces are, what they are for, how implementation happens.

What is interface and implementation

You have probably heard the word “interface” many times. For example, a computer has an input interface (mouse and keyboard), many programs have user interfaces. In the broadest sense, an interface is a link between two interacting parties. For example, the same keyboard or TV remote control. In programming, and in Java in particular, an interface is a specific contract that says what the class that implements it will do. An interface only defines behavior. It doesn't say anything about the object that will implement it. You can declare an interface in Java like this:

public interface MyInterface  {

     // constants declaration 
     // methods without implementation
     // static methods
     // default methods (default)
     // private methods
}
Here is a syntax of Implements in Java:

public class MyClass implements MyInterface{
//implementing the methods of MyInterface 
//Other code
} 
An interface describes behavior without specifying it. For example, a behavior such as "movement" can be applied to different types of objects: a bicycle, a person, a car, water in a river, and so on. The behavior of swimming can be the behavior of a duck, a ship, or a fish. These objects have nothing in common other than that they can move or swim. Yes, and the very movement with swimming they are very different. However, in Java you can create Duck, Boat, Fish classes and let them implement the ability to swim. This is where the Java Implements keyword is used.

Implement keyword Example


public interface Swimmable {
  
   void moveForward();
   void TurnRight();
   void TurnLeft();
  
}
As you can see, the methods themselves are not implemented. But we declared that the classes that implement this interface must be able to swim in a straight line, as well as turn right and left. Let's create classes that will implement this interface.

public class Duck implements Swimmable {
//implementing the methods
   public void moveForward() {
       System.out.println(" Quack, I am moving forward...");
   }

   public void TurnRight(){
       System.out.println("I am turning right...");
   }
   public void TurnLeft(){
       System.out.println("I am turning left...");

   }

   public void Stop() {
       System.out.println("Quack. I am relaxing on the surface of the water...");
   }

}

public class Fish implements Swimmable {

   public void moveForward() {
       System.out.println("I am moving forward...");
   }

   public void TurnRight(){
       System.out.println("I am turning right...");
   }
   public void TurnLeft() {
       System.out.println("I am turning left...");
   }

   public void turnUp(){
       System.out.println("I am turning up...");
   }

   public void turnDown(){
       System.out.println("I am turning down...");
   }

   public void Stop() {
       System.out.println("I am relaxing somewhere under the water surface...");
   }
}
All classes that implement the Swimmable interface, according to the contract, must be able to swim forward (implement the moveForward() method), as well as turn right and left. Implementation of these methods is required. A duck and a fish swim differently. Let's say a fish has two additional methods that implement its ability to swim up and down.The Swimmable interface doesn’t have this. However if we create a child of the Fish class, for example, Tuna or Salmon, they, like every “Fish”, will be able to swim up and down. Implements in Java - 1

Multiple Interfaces in Java

As you probably already know, Java doesn’t support multiple inheritance. That means that one class can be inherited from one superclass only. However in a way you still can use “multiple inheritance” in Java, because a class can implement multiple interfaces.

To implement multiple interfaces, use the next syntax: 
interface MyFirstInterface {
   public void myMethod();
}
interface MySecondInterface {
   public void myOtherMethod();
}

// MyClass implements both MyFirstInterface and MySecondInterface
class MyClass implements MyFirstInterface, MySecondInterface {
   public void myMethod() {
      //method implementation
   }
   public void myOtherMethod() {
     //method implementation
   }
}

Multiple Interfaces Example

Remember that a duck can not only swim, but also fly. Let's write the flight interface and implement it in our duck.

public interface Flyable {
   double startAge = 0.1;
   void fly();
}

public class Duck implements Swimmable, Flyable {

   public void moveForward() {
       System.out.println(" Quack, I am moving forward...");
   }

   public void TurnRight(){
       System.out.println("I am turning right...");
   }
   public void TurnLeft(){
       System.out.println("I am turning left...");

   }

   public void Stop() {
       System.out.println("Quack. I am relaxing on the surface of the water...");
   }

   public void fly(){
       System.out.println("I am flying!!!");
   }

}
And again, it is important to remember why we are writing the interface. Let's say if it is implemented by a bird, an airplane, a skydiver and a dandelion, their flights will be completely different.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION