Java - Interface (Class) - Data Encapsulation

Java Conceptuel Diagram

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:

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.

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.
Default Method declaration

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));
    }
}
Implementation

To declare a class that implements an interface, you include an implements clause in the class declaration.

Role
API

Interfaces are the basis element to define an APIs.

Multiple Inheritance

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.

Rewriting: interface that extends 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.

Documentation / Reference





Discover More
Code Refactoring
Code - Refactoring

The process of modifying the code structure, without modifying its current behavior, is called Refactoring. Refactoring is a method that search to improve the code quality of a program. code duplication...
Card Puncher Data Processing
Design Pattern - Data Access Object (DAO)

A data access object (DAO) is an object that provides an abstract interface to some type of database or persistence mechanism, providing some specific operations without exposing details of the database....
Card Puncher Data Processing
Design Pattern - Facade

API A facade API with a interface in Java Facade_pattern
Card Puncher Data Processing
How to develop a Task for Ant?

How to develop a Task for Ant A class is registered as an AnT task if it's provide a method with the signature “public void execute()”. The class doesn't need to extends no superclass and...
Java Conceptuel Diagram
J2EE - Session Bean

A session bean encapsulates business logic that can be invoked programmatically by a client over local, remote, or web service client views. To access an application that is deployed on the server, the...
Jdbc Class Architecture
JDBC (Java Database Connectivity)

JDBC – Java Database Connectivity is a Java API that provides access to most popular databases or to other tabular data sources. It provides methods for querying and updating data in a database. JDBC...
Java Conceptuel Diagram
JMX - Mbean (Managed Bean|Manageable resource)

A managed bean (MBean) is a Java object that represents a JMX manageable resource in a distributed environment, such as: an application, a service, a component or a device. An MBean is a namedmanaged...
Card Puncher Data Processing
JPA - API (Programming Model)

API JPA is a specification that deals with object/relational mapping and data persistence between Java and databases. When developing an application, you need to know how to use the following application...
Java Collection Type
Java - (Collection|container) Framework (Types)

A collection (known also as a container) is an object that groups multiple objects (elements) into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. ...
Java Conceptuel Diagram
Java - (Data Type|Type)

data type in the java world. The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. The language supports four kinds...



Share this page:
Follow us:
Task Runner