Quick Reach
What is interface in Java
The Interfaces are most commonly a group of related methods with no other code or functionality or with the empty bodies. Alternatively, you can say an interface is the blueprint of a class that implements it.
It needs to be differentiated that Java interfaces are not the classes. The Interface contains behaviours that a class implements whereas a class describe the methods (behaviours) and properties (attributes) of the objects.
An example of interface
To make it clearer, consider an example of the Car interface. An interface of the car may look like:
1
2
3
4
5
6
7
8
9
|
Public interface car {
void engine_power(int value);
void car_color (String string_value);
void car_gears (int value);
}
|
As such, the classes implement the interfaces, let us say a car company, Honda implements this car interface as follows:
1
2
3
4
5
6
7
|
Public Class Hondacar implements car {
//Class features (methods and attributes)
//it will inherit
}
|
So the class will at least implement the basic car requirements and may add its own.
In Java, the List is an interface of the Collection, which is an ordered collection of elements or dynamic array. Whereas the ArrayList, LinkedList classes implements the List interface.
A few main points about interface are:
- A class implements an interface, not extend it.
- The Interface can extend many interfaces.
- Interfaces in java cannot be instantiated.
- The Java Interfaces do not contain the constructors.
- It is the mechanism to achieve abstraction in Java (an object-oriented, OOP concept)
- Methods in interface Java are also abstract implicitly.
How to declare an interface
Enough of the theory, it is time to learn how to declare and run an example of creating the interface.
This is how you can declare an interface:
public interface interface_name
{
// Final, Static fields here
//Abstract methods go here
}
An interface example
This example shows how to create a Java interface with one method.
1
2
3
4
5
|
public interface test_interface {
void test();
}
|
So the test_interface contains one method test(). You have to save this as test_interface.java. Now we will create a class that implements the test_interface. See the code below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class interface_example implements test_interface{
public void test(){
System.out.println(“Hello World”);
}
public static void main(String args[]){
interface_example objinterface = new interface_example();
objinterface.test();
}
}
|
Save this file as interface_example.java.
Experience this online
The output will be:
Hello World
Inheritance in interface
Like classes, the Java interface can also inherit other interfaces. However unlike the class, an interface in java can inherit multiple interfaces.
See below, how to inherit single and multiple interfaces.
Single interface inheritance
You have to use the “extends” keyword to inherit interface in Java.
public interface child_interface extends parent_interface
Multiple interface inheritance
Multiple interfaces are inherited by using a ‘,’ (comma) after first parent class and so on. See below to learn how to extend multiple interfaces.
public interface child_interface extends parent_interface1, parent_interface2
Also see: Java ArrayList | Java Vector | Java array
Leave A Comment?