Interface in Java with Examples

As an object-oriented programming language, Java provides various features for programmers regarding the security of the code. One such feature is the abstraction that is attained via using an interface. This is where interface plays a major role in the Java programming language. This article will elaborately explain every other detail of Java Interface and Functional Interfaces.

Java Interface Methods:

Before moving into Functional interfaces, we must clearly understand the role of the interface in Java. Basically, it is a blueprint of a class. It contains static constants and various abstract methods in it.

The main role of an interface is to provide abstraction and multiple inheritances possible in Java.

More about Java interfaces:

  • It is an abstract data type that helps in identifying the behaviour of a class.
  • It also represents the IS-A relationship.
  • In the Java interface, only the abstract methods are allowed; no method body can be present. In other words, it specifies what a class would do but does not reveal how the class does it.
  • If a class implements an interface and does not reveal the body of a method for its functions, then that class is termed an abstract class.
  • The default and static methods in an interface exist from Java 8.
  • We also have private methods in an interface since Java 9.

Why should we use an interface?

  • It provides complete abstraction in an easy way.
  • We already know that Java does not support multiple inheritances like C++. This is possible using interfaces.
  • Loose coupling is also possible under an interface.

Abstract classes vs Interfaces in Java:

What special functionality does an interface provide when there is a separate concept called abstract class for abstraction?

Abstract class does not allow to declare non-final variables in it. In the case of an interface, the presence of final, public and static variables is possible.

  Class Interface
1. Variable initialization is possible. Variable initialization is not possible.
2. Various access specifiers like private, protected and the public are included. Only public access specifier can be implemented.
3. It contains a method with method body or implementation. It contains only the method, the method body or its implementations is not present.

Interface declaration in Java:

To declare an interface in Java, simply use the keyword interface. The methods in it are created with an empty body, and their fields are public, static and final. When a class implements an interface, it implements all the methods present in that interface.

Syntax to declare an interface:

interface interface_name{
// declare the constant fields
// declare the abstract methods 
}

Java 8 Interface Improvement:

Since Java 8, an upgrade in the interface has led to the presence of default static methods in it. This is further explained below:

Internal addition in the compiler:

Since JDK8, the Java compiler adds the keywords public and abstract before the interface method. It also adds other keywords like public, static and final before the variables.

interface improvement

Relationship between java classes and interfaces:

A class extends another class, and an interface extends another interface. At the same time, a class implements an interface.

relationship between classes and interfaces

Sample program to implement Java Interface:

interface Printcourse{
void print();
}
class FirstCode implements printcourse{
public void print(){
System.out.println(“The course is Java”);
FirstCode fc = new FirstCode();
fc.print();
}
} 

Output:

The course is Java

Multiple Inheritance in Java using Interface:

Multiple inheritance using interface takes place when a class implements multiple interfaces or an interface extends multiple interfaces. The diagram and code given below will make to understand it a bit more.

multiple inheritance in java using interface

Sample code to implement multiple inheritances:

interface Printcourse{
void print();
}
interface Displaycourse{
void display();
}
class FirstCode implements Printcourse,Showcourse{
public void print(){
System.out.println(“Java”);
}
public void display(){
System.out.println(“Learn Java with FirstCode”);
}
public static void main(String args[]){
FirstCode fc = new FirstCode();
fc.print();
fc.display();
}
}

Output:

Java
Learn Java with FirstCode

Now, this brings us to a question how come multiple inheritances are supported in Java via interface but not through class?

Multiple inheritances face restrictions to take place inside a class due to their ambiguity. Due to the absence of this ambiguity in an interface, multiple inheritances become possible in this case.

Look at the sample program below to understand it practically:

interface Printcourse{
void print();
}
interface Displaycourse{
void display();                                                                          
}
class FirstCode implements PrintCourse, DisplayCourse{
public void print(){
System.out.println(“Java”);
}
public static void main(String args[]){
FirstCode fc = new FirstCode();
fc.print();
}
}

Output:

Java

Here, the interfaces Printcourse and Displaycourse contain certain methods, but the implementation is provided by the class FirstCode. Therefore, there is no ambiguity, and thus multiple inheritances are possible.

Inheritance using interface:

interface Printcourses{
void print();
}
interface Pickcourse extends PrintCourses{
void pick();
} 
class FirstCode implements Pickcourse{
public void print(){
System.out.println(“The courses are: C, C++, Java, Python”);
}
public void pick(){
System.out.println(“The course I picked is: Java”);
}
public static void main(String args[]){
FirstCode fc = new FirstCode();
fc.print();
fc.pick();
}
}

Output:

The courses are: C, C++, Java, Python
The course I picked is: Java

JDK 8 Static Method in Interface:

interface Addcalc{
void calc();
static int num(int a, int b)
{
return a+b;
}
class Explanation implements Addcalc{
public void calc(){
System.out.println(“Addition of two numbers is”);
}
}
class FirstCode{
public static void main(String args[]){
Addcalc ac = new Explanation();
ac.calc();
System.out.println(Addcalc.num(5, 10));
}
}
}

Output:

Addition of two numbers is
15

Tagged interface or marker interface:

An interface that contains no member is a tagged interface or a marker interface. Some of the tagged interfaces are Serializable, Remote, Cloneable etc. These interfaces lend a lot of information to the JVM to undertake various useful operations.

public interface Serializable{}

Nested interface in Java:

When an interface contains another interface, this is known as a nested interface. Here is a simple nested interface example:

interface Printtext{
void print();
interface Printtext2{
void print2();
}
}

Summary

This was all about java interface, interface methods, interface declaration etc. Hope you liked the article.

Leave a Reply

Your email address will not be published. Required fields are marked *