Site icon TestingDocs.com

Java Interface with Examples

Overview

In this tutorial, we will learn about the Java interface. The interface keyword is used to declare and create an interface in Java. We will look at some code listings and examples from Selenium API.

Interface

In the real-world, an interface is a sub-system or a device that is used for communication between entities within the system. For example, a TV remote control is an interface between the user and the TV set.

 

Java Interface

In Java language, an interface is a type that defines the method signatures. It is a contract between the implementing Java class and the outside world. In the Java language, a class can implement multiple interfaces.

Java objects define the behavior and interactions with the outside world through the methods. These methods define the object’s interface. An interface defines the class’s behavior and interactions. It only contains the methods without implementations.

For example, a client using the application class, calling application code, etc. An interface body has only public method signatures and initialized final fields.

In other words, all the methods in the interface are abstract. All the fields in the interface are constants. An interface doesn’t provide method implementations. It’s up to the classes that implement the interface to provide implementations to the interface methods.

Example

 

// www.TestingDocs.com - Java Tutorials
public interface Draggable {
	public void dragBy();
}

// GUI Window
public class Window implements Draggable {

	@Override
	public void dragBy() {
		System.out.println("By Mouse");
	}

}

// Wooden Chair
public class Chair implements Draggable {

	@Override
	public void dragBy() {
		System.out.println("Pull By Hand");
	}

}

Notice that the classes that implement the interface have different implementations. The classes are also unrelated in the real world. The Window class is a GUI software application window and the Chair class is a wooden chair!

A concrete class that implements the interface should provide the implementations of the interface methods. An abstract class that implements the interface may delegate this responsibility of implementations to concreate sub-classes.

Selenium API Example

Now, let’s look at an example from the Selenium Webdriver API. (Application Programming Interface ). WebDriver is an interface in Selenium API. It is a remote control interface for the browsers. Notice that, WebDriver extends another interface called SearchContext. SearchContext is another interface.

 

public interface WebDriver extends SearchContext {

...

}

Empty Interface

Sometimes, an interface might not have any methods and fields. These are called empty interfaces or marker interfaces.  They just serve the purpose of semantics. For example,

java.io.Serializable interface

Any class to enable Serializability should implement this interface.

public class IamSearlizableClass implements Serializable {

/*
* 
*/
private static final long serialVersionUID = 1L;

...

}

 

Java directly doesn’t support multiple class inheritance. A class cannot extend multiple classes in Java. The main advantage of interface is that Java supports multiple interface inheritance. Multiple classes can implement the same interface and a single class can implement multiple interfaces.

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Exit mobile version