Java - Interface (Class) - Data Encapsulation
About
see Data Type - Interface (Abstract Type)
In the Java programming language, an interface is a reference type, similar to a class, that can contain only:
- and nested types.
There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.
Defining an interface is similar to creating a new class.
An interface defines a protocol of communication between two objects.
An interface declaration contains signatures, but no implementations, for a set of methods, and might also contain constant definitions.
A class that implements an interface must implement all the methods declared in the interface.
An interface name can be used anywhere a type can be used.
Articles Related
Syntax
Standard
An interface declaration consists of:
- modifiers,
- the keyword interface,
- the interface name,
- a comma-separated list of parent interfaces (if any),
- and the interface body.
For example, the buttons on the front of your television set, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the “power” button to turn the television on and off.
A television's behavior, if specified as an interface, might appear as follows:
public interface Television extends Interface1, Interface2, Interface3 {
// constant declarations
int InitialVolume = 1;
int InitialChannel = 1;
void changeChannel(int newValue);
void changeVolume(int newValue);
....
}
where:
- The public access specifier indicates that the interface can be used by any class in any package. If you do not specify that the interface is public, your interface will be accessible only to classes defined in the same package as the interface.
- All methods declared in an interface are implicitly public, so the public modifier can be omitted.
- All constant values defined in an interface are implicitly public, static, and final. Once again, these modifiers can be omitted.
An interface can extend other interfaces, just as a class can extend or subclass another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces. The interface declaration includes a comma-separated list of all the interfaces that it extends.
Generic
declaration of the Collection interface
public interface Collection<E>...
The
syntax tells you that the interface is generic. When you declare a Collection instance you can and should specify the type of object contained in the collection. Specifying the type allows the compiler to verify (at compile-time) that the type of object you put into the collection is correct, thus reducing errors at runtime.You can define a default method with a default body in an interface.
public interface TimeClient {
....
default ZonedDateTime getZonedDateTime(String zoneString) {
return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));
}
}
To declare a class that implements an interface, you include an implements clause in the class declaration.
Interfaces are the basis element to define an APIs.
Interfaces have another very important role in the Java programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. The Java programming language does not permit multiple inheritance, but interfaces provide an alternative.
In Java, a class can inherit from only one class but it can implement more than one interface. Therefore, objects can have multiple types:
- the type of their own class
- and the types of all the interfaces that they implement.
This means that if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.
If you make a change to the interface (add a function for instance), all classes that implement the old interface will break because they don't implement the interface anymore. Programmers relying on this interface will protest loudly.
Because this is often impossible to specify the interface completely from the beginning, you may need to create more interfaces later. For example, you could create a MyInterfacePlus interface that extends MyInterface:
public interface MyInterfacePlus extends MyInterface{
boolean didItWork(int i, double x, String s);
}
Now users of your code can choose to continue to use the old interface or to upgrade to the new interface.