1. Definition of Interface in Java

The term "interface" in Java encompasses various meanings, contingent upon the context and application. Broadly speaking, an interface can be perceived as a Service Requirement Specification (SRS).

1.1 Case 1: Java Servlet Implementation

Consider Tommy, who aims to develop an online calculator web service using a Java-based language. To enable his app to run in a web server environment, he must implement Java Servlet. Various implementors like Apache Tomcat, OHS, Resin, and WebLogic follow the Servlet API provided by SUN, a set of guidelines dictating the methods and classes they should implement. The Servlet API serves as an interface, representing the service requirements for Java servlet implementation.

1.2 Case 2: JDBC API for Database Communication

In another scenario, imagine a Java application needing to communicate with databases like Oracle and MySQL. To facilitate this communication, a driver acts as a translator. For each database, a specific driver with JDBC API implementation is necessary. If a new database, such as PostgreSQL, emerges, a corresponding driver with JDBC API is required for seamless integration. Creating a custom driver for a proprietary database involves referencing the JDBC API provided by SUN, making JDBC API an interface defining the service requirements for database communication.

Understanding the concept of interface in Java involves recognizing it as a set of specifications and guidelines, crucial for seamless integration and communication within various contexts.

2. Definition 2: Interface as a Contract

Beyond the Service Requirement Specification (SRS) interpretation, an interface in Java can also be conceptualized as a contract between a client and a service provider. To illustrate this concept, let's delve into an example.

Meet Mr. Allan, a client of X.Tech Company. In this scenario, Mr. Allan assumes the role of the client, and X.Tech is the designated service provider. Mr. Allan has a specific requirement—he wants X.Tech Company to develop a Parking Automation System (PAS). To communicate his expectations clearly, Mr. Allan compiles a list of services PAS should offer and presents this requirement to X.Tech Company. From Mr. Allan's perspective, this list represents the expected services in the PAS.

In response, X.Tech Company reviews the provided requirement and commits to implementing every service listed by Mr. Allan. The document containing Mr. Allan's specified services is what we refer to as an interface. In essence, the interface transforms into a formalized contract, outlining the agreed-upon terms between the client (Mr. Allan) and the service provider (X.Tech Company).

This analogy helps grasp the idea that an interface serves as a binding agreement, ensuring clarity and mutual understanding between the client and the service provider in the development process.

3. Understanding Interface Declaration and Implementation in Java

In this section, we delve into the intricacies of interface declaration and implementation in Java. Having discussed the essence of an interface, it's crucial to grasp the process of declaring and implementing it.

3.1 Interface Declaration

An interface is declared using the 'interface' keyword. It comprises abstract methods, implying that all methods in an interface are declared with an empty body. By default, all fields are public, static, and final.

3.2 Interface Implementation

To implement an interface, a class uses the 'implements' keyword. This class is then obligated to implement all the methods declared in the interface.

Example:

interface Interf {
  public void m1();
  public void m2();
}

class ServiceProvider implements Interf {
  // Mistake 1: Missing 'public' keyword
  void m1(){}

  // Mistake 2: Incomplete implementation (missing m2())
}

// Code above is invalid; contains mistakes

Common Mistakes and Solutions:

Mistake Number 1: Every abstract method in the interface is always public and abstract. If the method in the interface is public, the implementation in the class must also be declared as public. This concept is akin to overriding a parent class method in the child class.

Mistake Number 2: Incomplete implementation of methods. If possible, provide implementation for every method in the interface. If not, declare the class as an abstract class, and the responsibility to provide implementation for unimplemented methods falls on the child class.

Solutions:

// Solution 1: Provide implementation for all methods
class ServiceProvider implements Interf {
  public void m1(){}
  public void m2(){}
}

// Solution 2: Declare the class as an abstract class
class ServiceProvider implements Interf {
  public void m1(){}
}

In summary, two crucial rules govern interface declaration and implementation:

Rule 1: Every method in the interface should be declared as public. The implementation of these methods in the class should also be declared as public.

Rule 2: Strive to provide implementation for every method in the interface. If not feasible, declare the class as an abstract class, and let the child class handle the implementation of unimplemented methods.

End Of Article

End Of Article