Interfaces is also a key concept of java which is used very often in java applications. This tutorial explains different details of interfaces like what interfaces is, how to create interfaces, why do we use interfaces etc.
In java programming language an interface is a reference
type, similar as class, that can contain only constants, method
declaration(method signature, no body), default
methods,
static
methods and nested types inside it's body. Nested type simply means it can contain another interface or class inside it.
We will discuss this in nested interface tutorial.
The default
and
static
methods can be used from java 8 onward only. These methods must have a body if used in an
interface. These methods will be discussed in next tutorial. This tutorial will explain only basic component of an interface which is constants and method
declarations.
An interface in java is declared using interface
keyword, followed by interface name. After interface name it's interface body enclosed
in { }. The basic syntax of declaring an interface is :
interface
interfaceName {// constant declarations
// Method signatures
} Example :interface
MyInterface {int
id = 20;void
print();public int
calculateArea(); }
The name of interface is given by the programmer by following the java naming convention. Inside the interface we can declare variables and methods. A variable declared inside an interface must be initialized with some value.
We can also use public
and abstract
modifier with interface declaration. An interface declared with public
access modifier is accessible everywhere while the abstract
modifier is added implicitly by the compiler with each interface that we declare.
public abstract interface
MyInterface {// constant and Method declarations
}
Interfaces in java cannot be instantiated, which means we can not create the object of interfaces using new
keyword. Interfaces can only be
implemented by classes or extended by other interfaces.
How to implement interface in Java
Interfaces are designed to be implemented by classes as they contains method declarations only, no method body. These methods must be
defined by classes which implements the interface. The class that implements an interface
must define all the methods of that interface otherwise that class
must be defined as abstract
class.
Java provides implements
keyword which is used to implement an interface
by a class. The program below demonstrates how a class implements an interface.
Interface program in Java
interface
MyInterface {int
id = 20;void
print(); }class
MyClassimplements
MyInterface {public void
print() { System.out.println("This is print method"
); }public static void
main(String args []) { MyClass obj =new
MyClass(); obj.print(); System.out.println(obj.id); } }
Save above program as MyClass.java
, compile as javac MyClass.java
and run as
java MyClass
.
Output:
This is print method
20
How to save interface program ?
If a program contains only interface then it should be saved by interface name. If the program
contains class and interface both then it must be saved by the class or interface name declared with public
modifier.
If it doesn't contain any public
interface or class then prefer to save by class name, but it's not mandatory.
What does java creates after compilation of an interface ?
Java compiler always creates a .class
file for every interface, no matter it is
defined separately or in combination with class inside a program.
Each constants defined inside an interface are by default public
,
static
and final
while each methods are by default public
and abstract
. It's the compiler which adds these modifiers with constants and variables after compilation of interface.
For example the above interface
would be converted as below :
interface
MyInterface {public static final int
id = 20;public abstract void
print(); }
So it doesn't matter whether you use or not use these modifiers with variables and methods, java compiler will automatically adds these modifiers after compilation of your interface.
Can I declare a variable of interface as private or protected ?
No, the variable of an interface must be declared either public
or it should
be left without any modifier.
Implementing multiple interfaces
Java allows the classes to implement multiple interfaces. If a class implements multiple interfaces, then it must implement all the methods of each interfaces. The Program below demonstrates multiple interface implementation.
Java program to implement multiple interfaces
interface
FirstInterface {int
id = 20;void
printMessage(); }interface
SecondInterface {int
count = 10;void
printDetail(); }class
MultipleInterfaceimplements
FirstInterface, SecondInterface {public void
printMessage() { System.out.println("This is printMessage method"
); }public void
printDetail() { System.out.println("This is printDetail method"
); }public static void
main(String args []) { MultipleInterface obj =new
MultipleInterface(); obj.printMessage(); obj.printDetail(); System.out.println("id = "
+obj.id); System.out.println("count = "
+obj.count); } }
Output:
This is printMessage method
This is printDetail method
id = 20
count = 10
When to use interfaces in Java
Interfaces are a way of defining a set of contracts(in form of method) or minimum functionalities of an application. While designing an interface in your application, think it in this way, what are the minimum functionalities your application/software must have. You can write those functionalities in the form of abstract methods in one or more interfaces. Classes dealing such functionalities can implement the corresponding interface to ensure to provide the required functionalities by implementing the methods of that interface.
For an example if you are developing a user management software which will provide the functionalities like add user, update user, delete user, get user
etc, you can
write them in the form of methods inside an interface. The classes dealing such functionalities can implement that interface to provide these functionalities.
Why do we use interface in Java
Interfaces are generally used to declare a set of contracts or minimum functionalities which must be present in an application or program. These contracts are then implemented by classes to provide the actual feature. Apart from this there are some more benefits as listed below, of using interfaces in java.
- Interfaces helps to achieve abstraction as it contains method declarations only, no method body.
- Using interfaces we can achieve multiple inheritance in java, as a class can implement multiple interfaces.
- It helps to achieve loose coupling, as methods are declared and defined separately.
- Interfaces helps to achieve polymorphism, as a class can behave as multiple types by implementing multiple interfaces.
You may not understand the actual use of interfaces in a single program but once you start building any application or library designing, you would understand the use of interfaces in java.
Can we design an application without the use of interfaces ?
Yes you can design an application without using any interface, but a good design strategy is to use interfaces in your application where ever needed.
- An interface acts as non primitive data type, as it's a reference type.
- A program can declare multiple interfaces inside it.
- Object of implementing class can be assigned in to implementing interface type.
- A public class and public interface can not be declared together inside a program.
- Always remember to apply public access modifier while implementing an interface method in a class.