JAVA INTERFACE A POWERFUL DEVELOPER TOOL
A few years ago we traveled to France for spring break. In preparation, we had to get a few things. For instance, some of the electronic devices needed an adapter. The plug-in was slightly different. A common interface is really helpful for electrical power. Without this common standard like the Java Interface, we would have to have different cords for so many things. Kind of like our cellphones already do! Don’t get me started…
Java allows developers to set a standard without defining the underlying class. This is called a Java Interface. We just declare the method signature and fields and leave it to the class that implements it to handle the hard work. There is an amazing amount of value here. Let’s first explore how we can get started.
What is a Java Interface?
Similar to a full-fledged Java class an interface lets us define the method signature. If you were a baker you could tell a customer give me a cup of flour, two cups of sugar and one egg. In return, I will give you a dozen cookies. We don’t have to worry about how the cookies are made. We just know what we have to give the baker. Sometimes when we develop it is better to know the signature and leave the implementation for later.
Why use a Java Interface?
As we spoke about in the previous paragraph with the baker example. We don’t need to worry about how the sausage is made. Just what we pass to the method. An interface can be helpful for using other systems. They can provide the interface and you “program to the interface.” Then if they want to change how they do the process your code that calls it is not affected.
What do they look like?
The interface in Java is quite simple. It is just like a class without the method body. Let’s compare the two with some simple examples.
Interfaces
There are a number of situations in software engineering when it is important for disparate 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.
For example, imagine a futuristic society where computer-controlled robotic cars transport passengers through city streets without a human operator. Automobile manufacturers write software (Java, of course) that operates the automobilestop, start, accelerate, turn left, and so forth. Another industrial group, electronic guidance instrument manufacturers, make computer systems that receive GPS (Global Positioning System) position data and wireless transmission of traffic conditions and use that information to drive the car.
The auto manufacturers must publish an industry-standard interface that spells out in detail what methods can be invoked to make the car move (any car, from any manufacturer). The guidance manufacturers can then write software that invokes the methods described in the interface to command the car. Neither industrial group needs to know how the other group’s software is implemented. In fact, each group considers its software highly proprietary and reserves the right to modify it at any time, as long as it continues to adhere to the published interface.
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. Interfaces cannot be instantiatedthey 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 < // constant declarations, if any // method signatures // An enum with values RIGHT, LEFT int turn(Direction direction, double radius, double startSpeed, double endSpeed); int changeLanes(Direction direction, double startSpeed, double endSpeed); int signalTurn(Direction direction, boolean signalOn); int getRadarFront(double distanceToCar, double speedOfCar); int getRadarRear(double distanceToCar, double speedOfCar); . // 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 OperateBMW760i implements OperateCar < // the OperateCar method signatures, with implementation -- // for example: public 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 >// other members, as needed -- for example, helper classes not // visible to clients of the interface >
In the robotic 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. The guidance manufacturers, who are the clients of the interface, will build systems that use GPS data on a car’s location, digital street maps, and traffic data to drive the car. In so doing, the guidance systems will invoke the interface methods: turn, change lanes, brake, accelerate, and so forth.
Interfaces as APIs
The robotic car example shows an interface being used as an industry standard Application Programming Interface (API). 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 secretin 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.
Интерфейсы в Java. Полное руководство
Интерфейсы являются основной частью языка программирования Java. Кроме JDK они используются еще и в шаблонах проектирования, а также множестве различных инструментах и фреймворках. Также интерфейсы обеспечивают абстракцию в Java.
Например, нам нужно создать рисунок, который состоит из нескольких фигур. Для этого мы можем создать интерфейс Shape и определить методы для работы с объектами Shape. Для простоты, давайте определим только два метода — draw() – чтобы нарисовать фигуру и метод getArea() , который будет возвращать площадь фигуры.
Пример Java интерфейса
На основании вышеуказанных требований, наш интерфейс Shape будет выглядеть следующим образом:
При подготовке материала использовались источники:
https://medium.com/@TomHenricksen/java-interface-a-powerful-developer-tool-7ba8c9a7f5bd
https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html