A nice note on Java interface in which interface variables, methods has been clearly explained with declaration and examples. 3 complete interface examples and interface FAQs has been included with answers.
Here is the topics on interface in java that will be covered in notes
- What is interface in java and uses
- Naming Convention of interface in Java
- Java interface variables
- Java interface methods
- Simple Example of interface in java
- Interface extends another interfaces example
- Example of a class implementing 3 interfaces
- Java Interface FAQ’s
What is interface in java?
An interface in java is a collection of abstract methods and or static and final variables. All the methods in an interface must be unimplemented and abstract to force the sub classes to implement it. So, the class who implements the interface, it must implement by all methods.
To declare an interface, we use interface keyword. Syntax of declaring an interface is given below.
interface IShape{
}
In below figure, an interface A has a variable PI and a method f(). Class B is implementing and overriding the f() method of interface A.
Why to use interfaces in Java?
We use Interfaces in java to achieve multiple inheritance in java programs and loose coupling in java or say abstraction as well.
Naming Convention of interface in Java
The name of an interface in java should be in camel case with starting letter in upper case. That’s it. For example, Set, Map and SortedSet (CamelCase) etc.
Mainly, in java application, we use naming convention for code readability purpose. Some people try to write interface name by prefixing with capital letter “I” e.g. IShape etc. But it violates the readability. In fact, name the interface what it is. For example, Shape interface.
If you have a look on java libraries source code, you should find the naming convention in camel case with first letter in upper case.
- The name of an interface in java should be in camel case and with starting letter in upper case
- Interface names cannot start with a number.
.
Java Interface variables
In an interface, variables are static and final by default. And can have only public access modifier.
For example, here is a string variable declared in the interface.
syntax to declare variables within an interface.
interface IShape{
String shapes ="geometrical shape";
}
Compiler by default understand it as
interface IShape{
public static final String shapes ="geometrical shape";
}
Java Interface methods
Interface in java contains only declaration of methods. By default, All the methods are public and abstract and they cannot have definition or implementation.
All methods of an interface must be overridden and implemented by the classes who implement the interface. In fact, abstract keyword forces the sub classes to implement them. If you miss any method implementation in subclass, the compiler will throw an error.
Interface method declaration in java
interface A {
void f1();
void f2();
void f3();
}
Simple Example of Interface in Java
In below java interface example, class Test will implement the method declared in interface A. Note that the class uses implements keyword in java to implement/inherit an interface.
interface A {
int addNumbers(int a[]);
}
//Implement interface A and implement the interface method
public class Test implements A {
public static void main(String... args) {
A test = new Test();
System.out.println("Total of numbers in passed array is:"
+ test.addNumbers(new int[] { 1, 2, 3 }));
}
public int addNumbers(int[] a) {
int total = 0;
for (int i = 0; i < a.length; i++) {
total += a[i];
}
return total;
}
}
Interface Extends another Interfaces example
in java, an interface can inherit/extends another interface. For example below, interface B is extending the interface A. So, the class who implement interface B, all methods from interface B and interface will be available to the class to implement it.
interface A {
void fa();
}
interface B extends A {
void fb();
}
In fact, an interface can extend multiple interfaces like below.
interface A {
void fa();
}
interface C {
void fc();
}
interface B extends A,C {
void fb();
}
Java Code – Interface extends another interface
In the java interface example, 2 interfaces has been created i.e. A and B. Interface B extends interface A. The class Implementer will implement only interface B. The methods of interface A will be also available to the class.
interface A
{
void fa();
}
//interface B inherits interface B
interface B extends A
{
void fb();
}
//Class need to implement only interface B.
//method of interface A will be available to class
class Implementer implements B{
@Override
public void fa() {
System.out.println("Interface A:fa()");
}
@Override
public void fb() {
System.out.println("Interface B:fb()");
}
}
/**
* Test interface example
*
*/
public class Test {
public static void main(String... args) {
Implementer obj = new Implementer();
obj.fa();
obj.fb();
}
}
Output:
Interface A:fa()
Interface B:fb()
Example – A class implementing 3 interfaces
In below program, the class Implementer has implemented all 3 different interfaces A,B,C . In main program the object of the class has been created and invoked all methods.
Also, this example depicts that we can create a reference of an interface and assign object the class and invoke the method. But, in this case, all methods available in the interface for which you are creating a reference will be visible.
interface A {
public String displayA();
}
interface B {
public String displayB();
}
interface C {
public String displayC();
}
// class Implementer has inherited/implemented all methos of interfaces.
class Implementer implements A, B, C {
@Override
public String displayC() {
return "I was invoked from interface C";
}
@Override
public String displayB() {
return "I was invoked from interface B";
}
@Override
public String displayA() {
return "I was invoked from interface A";
}
}
public class Test {
public static void main(String... args) {
/***
* You can create the object of the class
* and call all methods.
*/
Implementer obj = new Implementer();
obj.displayA();
obj.displayB();
obj.displayC();
// You can also create reference of a interface and
// assign object of the class.
// In this case particular interface method will be visible.
A a_ref = new Implementer();
System.out.println(a_ref.displayA());
B b_ref = new Implementer();
System.out.println(b_ref.displayB());
C c_ref = new Implementer();
System.out.println(c_ref.displayC());
}
}
Java Interface FAQ’s
Q)Can interfaces be instantiated?
Answer: No interfaces cannot be instantiated like classes.
Q) Can an interface extend a class?
Answer: No, interfaces cannot extend classes.
Q)Can it also have a constructor?
Answer: Since, Interfaces don’t have methods implemented and are abstract, so, there is no meaning of having constructor in it.
Q) Can an interface be final?
No, an interface in java cannot be final. An interface is a pure abstract class. Hence, all methods in an interface are abstract that must be implemented by child classes. The purpose of final is to stop inheritance. So, it is contradictory that by using final interface, we want to stop it to be extended and saying child classes to inherit it. You can read more about final keyword in java where examples has been demonstrated.
Q) Can an interface implement another interface?
No, interfaces in java extends another interface not implement them.