INTERFACES IN JAVA
INTRODUCTION
We introduce interfaces in the context of OOP programming. At the beginning of the course, one of the aspects we focused on was the public interface. One of the basic principles by which we must design our classes is that of encapsulation. In particular, we must make attributes private, as well as methods used only by the class, and expose to the outside world the minimum functionality for those who will use our class.
ABSTRACT CLASSES
Often a set of responsibilities can be identified, defined in a general way, for example, with an abstract class, and implemented concretely within a number of different classes.
This represented in the figure is already a good starting point; however, sometimes there is a need to identify and represent a range of responsibilities that actually cut across a single generalization hierarchy, that is, we would like to be able to define a set of methods that represent a well-defined concept. This is where the interface, similar to abstract classes as they contain only the definition of a set of methods, which will then have to be implemented, regardless of generalization across all the classes that implement our interface.
A class can extend only one superclass but can implement as many interfaces as it wants by redefining the methods defined in the interfaces.
DECLARATION OF AN INTERFACE
Let’s look at how to declare an interface in Java.
We need to put the interface in a package with the file having the same name as the interface and the extension .java. We can also omit the public modifier, in which case the interface is visible only at the package level. There is a convention that interface names should begin with a capital i. In most cases an interface has a body that declares a set of methods but does not give an implementation.
The implementation of all methods is left to the classes that will implement this interface. All methods in interfaces are public, so it does not accorre to specify the access modifier. We can also exceptionally define constants within interfaces, and these will automatically have the public final static modifier associated with them. They are constants, so you need to initialize them and use the capital letter convention.
IMPLEMENTATION OF INTERFACES
A class that implements one or more interfaces can also have a superclass.
Let’s look at an example.
AClass conforms to the interface in that it implements the only method defined in the interface itself. Let’s look at the test class. One of the most important aspects in the use of interfaces is highlighted in yellow.
We can declare a variable of a certain interface type, just as we do with classes, and we can initialize it with a class as long as that class implements it. Obviously the method defined in the class is called.
If we want, we can also use this other syntax.
LINKS TO PREVIOUS POSTS
LINK TO CODE ON GITHUB
EXECUTION OF THE EXAMPLE CODE
- Download the code from GITHUB, launch the JAR file with the following command in Visual Studio Code, locating in the directory containing the JAR.
java -jar –enable-preview CorsoJava.jar
- Or run the main found in the file CorsoJava.java.
Leave A Comment