Interface in Java:
An Interface in Java is a class that does not have concrete methods. Instead, it contains all methods as abstract, providing a clear and distinct structure for your code. Understanding interfaces is essential for Java programmers as it is a fundamental concept in programming.
An interface is a class type without concreteness. It’s a pure abstract declaration. We cannot create objects for an interface. The subclass must implement all interfaces. A subclass can implement any number of interfaces. It is the subclass’s responsibility to provide an implementation for all those abstract methods of an/all interface(s). Otherwise, the subclass implicitly becomes an abstract class if it does not provide an implementation for at least one abstract method of any of those interfaces.
An interface can have abstract methods alone and cannot have constructors, but its data members are all implicitly static and final.
Abstract Class:
Classes contain methods. There are two types of methods: concrete methods and abstract methods. Concrete methods have a body, meaning they contain the actual code. On the other hand, abstract methods do not have a body.
Concrete methods implement them.
The method body is inside curly braces ({ }).
Among the methods, if at least one method is abstract, the class becomes an abstract class.
Explain Interface in Java:
It is a pure abstract declaration.
Abstract: means it has a method but without a body and terminates with a semicolon.
For example, a Microsoft Word document has a print icon that helps print the document. Think of this print icon as a method. However, the user cannot print with the print option alone. Notably, a printer connected to the system is necessary to print. In this example, the print icon can be considered a method and the printer as a body.
How to Create Interface in Java?
Creating an interface in Java is a straightforward process that any programmer can handle. While writing the program, the programmer specifies it as an interface instead of a class. Notably, an interface contains only abstract methods. Moreover, any class can quickly implement the interface, allowing the programmer to exercise more flexibility while designing the code. It’s a powerful tool that enables programmers to build the code of their choice.
Implements is a keyword for the interface.
Example of Interface in Java
Some examples of interface in Java include,
- Closable
- Runnable
- Auto closable
- Iterable
- Iterator
- List Iterator
- Random Access
- Collection
- List
- Set
- Sorted Set
- Map
- Sorted Map
- Queue
- Deque
- Comparable
- Comparator
Implementing Interfaces in Java
It gives the subclasses the choice to provide their interfaces.
One class can implement more than one interface.
Abstract Class and Interface
The abstract
class generally contains two types of methods: abstract and concrete.
In contrast, in the interface, only abstract
methods are present.
Iterable Interface in Java
The iterable interface is highly prominent in Java. It allows the programmer to iterate over a collection of objects, fetching the elements sequentially, one by one.
The other meaning of iterates is reading one by one objects.
In general, it is elements, while in object-oriented programming languages such as Java, it is objects.
Example for collection:
The array list is nothing but a list of elements.
To read a group of elements, we go for iterate. Through iterate, we fetch elements sequentially.