There are a number of situations in software engineering when it is important for different groups of programmers to agree to a "contract" that spells out how their software interacts.
Each group should be able to write their code without any knowledge of how the other group's code is written.
Generally speaking, interfaces are such contracts.
Interfaces in Java In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.
Method bodies exist only for default methods and static methods. Discussed later!
Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces. Extension is discussed later in this lesson.
Defining an interface is similar to creating a new class:
public interface OperateCar {
int turn(double direction,
double radius,
double startSpeed,
double endSpeed);
......
// more method signatures
}
Note that the method signatures have no braces and are terminated with a semicolon.
To use an interface, you write a class that implements the interface.
When an instantiable class implements an interface, it provides a method body for each of the methods declared in the interface. For example,
public class OperateBMW implements OperateCar {
int signalTurn(Direction direction, boolean signalOn) {
// code to turn BMW's LEFT turn indicator lights on
// code to turn BMW's LEFT turn indicator lights off
// code to turn BMW's RIGHT turn indicator lights on
// code to turn BMW's RIGHT turn indicator lights off
}
}
In the car example above, it is the automobile manufacturers who will implement the interface.
Chevrolet's implementation will be substantially different from that of Toyota, of course, but both manufacturers will adhere to the same interface.
APIs are also common in commercial software products.
Typically, a company sells a software package that contains complex methods that another company wants to use in its own software product.
An example would be a package of digital image processing methods that are sold to companies making end-user graphics programs.
The image processing company writes its classes to implement an interface, which it makes public to its customers.
The graphics company then invokes the image processing methods using the signatures and return types defined in the interface.
While the image processing company's API is made public (to its customers), its implementation of the API is kept as a closely guarded secret—in fact, it may revise the implementation at a later date as long as it continues to implement the original interface that its customers have relied on.
interface in1
{
void display();
}
class testClass1 implements in1
{
public void display()
{
System.out.println("Test class 1");
}
}
class testClass2 implements in1
{
public void display()
{
System.out.println("Test class 2");
}
}
class Main
{
public static void main (String[] args)
{
in1 t1 = new testClass1();
t1.display();
in1 t2 = new testClass2();
t2.display();
}
}
Output:
Test class 1
Test class 2